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!');
}

しかし、ドロップダウンの 1 つが空の場合、スクリプトのそれ以上の実行を防ぐことはできません。Event.stop(e) は、入力フィールドの 2 番目の部分のみで機能するようです。

望ましい動作:

  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 に答える