0

これが私のコードです:

    $('input#price_match_submit').click(function(event) {
        if ($.trim($('input#price_match_competitor_price').val()) == '') {
            alert("Please enter competitor's price.");
            return false;
        }
        if ($.trim($('input#price_match_name').val()) == '') {
            alert("Please enter your name.");
            return false;
        }
        if ($.trim($('input#price_match_quantity').val()) == '') {
            alert("Please enter the quantity.");
            return false;
        }
        if ($.trim($('input#price_match_email').val()) == '') {
            alert("Please enter your email address.");
            return false;
        }
        if ($.trim($('input#price_match_competitor_website').val()) == '') {
            alert("Please enter competitor's website.");
            return false;
        }
        if ($.trim($('input#price_match_phone_number').val()) == '') {
            alert("Please enter your phone number.");
            return false;
        }
    });

こちら#price_match_submitが送信ボタンです。ボタンをクリックすると、この関数が実行され、フォームが検証されます。しかし、期待どおりに機能していません。フォームは検証なしで送信されています。私はどこで間違っていますか?

4

4 に答える 4

0
$(#price_match_submit').click(function(event) {
   if ($.trim($('input#price_match_competitor_price').val()) == '') {
        alert("Please enter competitor's price.");
        event.preventDefault();
    }
    else if ($.trim($('input#price_match_name').val()) == '') {
        alert("Please enter your name.");
        event.preventDefault();
    }
    else if ($.trim($('input#price_match_quantity').val()) == '') {
        alert("Please enter the quantity.");
        event.preventDefault();
    }
    else if ($.trim($('input#price_match_email').val()) == '') {
       alert("Please enter your email address.");
       event.preventDefault();
    }
    else if ($.trim($('input#price_match_competitor_website').val()) == '') {
        alert("Please enter competitor's website.");
        event.preventDefault();
    }
    else if ($.trim($('input#price_match_phone_number').val()) == '') {
        alert("Please enter your phone number.");
        event.preventDefault();
    }

    // if nothing happened until now, the form will be submited
});
于 2013-05-28T12:16:29.483 に答える
-1

すべての回答とコメントに感謝します。

このコードは正常に動作します。コードの他の部分に問題がありました。

要素に機能を割り当てると、機能はその物理要素にのみ割り当てられます。しかし、要素に物理的な変更を加えると、以前のすべてのプロパティが失われます。私のコードでは、この html をモーダル ポップアップで表示していました。同じ要素を使用する代わりに、html をコピーしていました。そのため、この関数とのバインディングが失われました。これが、このコードが機能しなかった理由です。

于 2013-05-28T12:15:35.980 に答える