【问题标题】:jquery one field from two is empty, form submitting blockedjquery 两个中的一个字段为空,表单提交被阻止
【发布时间】:2014-09-13 17:40:34
【问题描述】:

当两个字段都未填写后显示错误消息,然后我填写第二个(#phone)时,表单仍然被阻止。第一次发送时表单也被阻止,甚至第二个字段也被填充。当第一个字段 (#email) 不为空时,始终提交表单。

<form action="http://m.onet.pl" id="form" method="post">
<ul style="list-style:none">
    <li>
    <input type="text" id="email" name="email" placeholder="E-mail" /> 
    <input type="text" id="phone" name="phone" placeholder="Phone" /> 
    </li>
<li><button type="submit" name="sendad">Send form</button></li>
</ul>
</form>

jQuery

    $("#form").submit(function(e){
        if(($('#email').val().length === 0 || $('#phone').val().length === 0)) {
            e.preventDefault();
            $('#msg').html("One of two fields is reuired");
        } 
    });

论点有什么问题?

http://jsfiddle.net/1do6332d/1/

【问题讨论】:

    标签: jquery forms validation field


    【解决方案1】:

    http://jsfiddle.net/khahu9gy/

    简单的错误:使用&amp;&amp; 而不是||

    $("#form").submit(function(e){
        if(($('#email').val().length === 0 && $('#phone').val().length === 0)) {
            e.preventDefault();
            $('#msg').html("One of two fields is required");
        } 
    });
    

    【讨论】:

    • 不可能! :)) 为什么我没有遇到检查其他运营商 :)) 谢谢你的伙伴...我浪费了很多时间因为愚蠢的运营商 :P
    • 是的。当我第一次尝试接受时,必须等待 8 分钟 :) 点赞我的问题,这样我就可以获得一些声誉点。没有这个,我可以点赞主题:P
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-23
    • 2016-03-31
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多