1

我正在努力在提交之前检查表单字段的功能。我有一些选择(下拉字段)和一个文本字段。他们都不应该是空的提交。

脚本http://jsfiddle.net/6KY5J/2/重现。

我检查 .each 和其他文本字段中的下拉字段。这是功能:

function checkFields(e) {
    $$('.dropdown').each(function (element) {
        if (element.selectedIndex === 0) {
            alert('Fill all dropdown fields!');
            Event.stop(e);
            throw $break;
            return;
        }
    });

    if ($('sometext').value == '') {
        alert('Fill the input!');
        Event.stop(e);
        return;
    }
    alert('OK!');
}

但是,如果下拉列表之一为空,我将无法阻止脚本的进一步执行。Event.stop(e) 似乎仅在第二部分适用于输入字段。

期望的行为:

  1. 检查下拉菜单,如果其中一个为空,则停止执行,不再进行任何检查
  2. 仅当下拉列表不为空时才检查文本输入字段。
  3. 仅当所有内容都已填写时才提交。

问题出在第 1 步。我的脚本不会在此处停止,发出警报,但不会停止。任何想法?谢谢!

4

1 回答 1

0
function checkFields(e) {
    var dropdownsokay = true;
    $$('.dropdown').each(function (element) {
        if (dropdownsokay && element.selectedIndex === 0) {
            alert('Fill all dropdown fields!');
            Event.stop(e);   
            dropdownsokay = false;         
        }
    });

    if(dropdownsokay) { //only check textfield if all the dropdowns are okay
        if ($('sometext').value == '') {
            alert('Fill the input!');
            Event.stop(e);
            return;
        }
        alert('OK!');
    }

}
于 2013-05-31T10:53:20.753 回答