微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

返回false,但仍发布到下一页

如何解决返回false,但仍发布到下一页

| 我有一组3个复选框,如果没有选中,它将返回false和警告消息。 提交表单时,我收到警告消息“您必须检查至少一种颜色!”,然后该函数将返回false。但是,它仍将发布到下一页。我想它会提示我选中该复选框。 有人能帮我吗!
<FORM ACTION=\' ||cur_page||\' METHOD=\"POST\" class=\"myform\" id=\"myform\"  >


<p><label>Select color<em>*</em></label><div class=\"required\">
<input type=\"checkBox\" name=\"p_red\" id=\"p_red\" class=\"require-one\" value=\"RED\" /> RED </p>
<p><label>&nbsp;</label><input type=\"checkBox\" name=\"p_blue\" id=\"p_blue\" class=\"require-one\" value=\"BLUE\"  /> BLUE </p>
<p><label>&nbsp;</label><input type=\"checkBox\" name=\"p_yellow\" id=\"p_yellow\" class=\"require-one\" value=\"YELLOW\"  /> YELLOW </P></div>

<div><input type=\"submit\" value=\"Pay By Credit\" name=\"RTS_Button\" class=\"button red\"  ></div>


<SCRIPT>
$(document).ready(function() {     
    $(\"#myform\").submit(function() {         
        var $fields = $(this).find(\'\'input:checkBox[id=\"p_color\"]:checked\'\');         
        if (!$fields.length) {             
            alert(\"You must check at least one color!\");             
            return false; // The form should *not* submit         
         }     
      }); 
});

</SCRIPT> 
    

解决方法

.length
返回数字 尝试以下方法:
     if ($fields.length === 0) {             
        alert(\"You must check at least one color!\");             
        return false; // The form should *not* submit         
     }   
    ,尝试以下方法:
    <form action=\"javascript:sendForm();\">
而对于您的脚本:
    function submitForm() {
        var $fields = $(this).find(\'\'input:checkbox[id=\"p_color\"]:checked\'\');         
        if (!$fields.length) {             
            alert(\"You must check at least one color!\");             
            return false; // The form won\'t submit
         }
         else {
             //Run an ajax call from here to POST your data
             alert(\"Ajax worked (or not),but the fields were checkd\");
         }
不过,这是一种不同的方法/ UX。     

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。