如果这两个值都有效,如何避免表单提交

发布于 2024-11-07 14:19:53 字数 2860 浏览 3 评论 0原文

您好,下面是我用于表单验证在两个输入字段中输入的 ID 的代码。所有代码都工作正常。唯一的问题是,如果两个字段都无效,因为我正在禁用和启用,如何停止表单提交如果记录返回为 true,则提交按钮。但如果任何一个字段有效,则提交按钮将启用并允许用户提交表单。

<form>
<input type="text" id="1" value=""/>
<input type="text" id="2" value=""/>
<input type="submit" value="submit"  id="submitbutton" >
</form>
<script type="text/javascript">
            $$(document).ready(function() {             
            $$('#1').blur(function() {              
            var id = $$('#1').val();             
            $$.getJSON("user.cfc?method=getRequests&returnformat=json&queryFormat=column",{"status":id}, function(res1,code) {

                if(res1.ROWCOUNT > 0){  

                  for(var i=0; i<res1.ROWCOUNT; i++) {                      
                    s = "<span class='dPostTxt'>" + res1.DATA.ID[i] + " " + res1.DATA.FIRST_NAME[i] + " " + res1.DATA.LAST_NAME[i] + " " + "is valid." + "</span>";
                  };                  
                 $$('#submitbutton').removeAttr('disabled');//now id is valid so enabling submit button                  
                } 
                else {
                  $$('#submitbutton').attr('disabled', 'disabled'); //error with id so disabling button 
                  alert(" ID Is Not Valid.Please Enter Valid ID");
                  var s = "Entered  ID Is Not Valid.";                                  
                }     

                $$("#results").html(s);                
                },"json");                  
           });  }); 
    </script>

<script type="text/javascript">
            $$(document).ready(function() {              
            $$('#2').blur(function() {              
            var id = $$('#2').val();             
            $$.getJSON("user.cfc?method=getRequests&returnformat=json&queryFormat=column",{"status":id}, function(res,code) {

                if(res.ROWCOUNT > 0){  

                  for(var i=0; i<res.ROWCOUNT; i++) {                   
                    s = "<span class='dPostTxt'>" + res.DATA.ID[i] + " " + res.DATA.FIRST_NAME[i] + " " + res.DATA.LAST_NAME[i] + " " + "is valid." + "</span>";
                  };
                 $$('#submitbutton').removeAttr('disabled');//now id is valid so enabling submit button                  
                } 
                else {                  
                  $$('#submitbutton').attr('disabled', 'disabled'); //error with id so disabling button 
                  alert(" ID Is Not Valid.Please Enter Valid ID");
                  var s = "Entered  ID Is Not Valid.";                                  
                }     

                $$("#results1").html(s);                
                },"json");                  
           });  }); 
    </script>

Hi Below is the code that i am using for my form to validate id's entered in two input fields.All code is working fine.Only thing is that how to stop form from submitting if both the fields are not valid as i am disabling and enabling submit button if record return is true.but if any one field is valid then submit button is enabling and allowing user to submit form.

<form>
<input type="text" id="1" value=""/>
<input type="text" id="2" value=""/>
<input type="submit" value="submit"  id="submitbutton" >
</form>
<script type="text/javascript">
            $(document).ready(function() {             
            $('#1').blur(function() {              
            var id = $('#1').val();             
            $.getJSON("user.cfc?method=getRequests&returnformat=json&queryFormat=column",{"status":id}, function(res1,code) {

                if(res1.ROWCOUNT > 0){  

                  for(var i=0; i<res1.ROWCOUNT; i++) {                      
                    s = "<span class='dPostTxt'>" + res1.DATA.ID[i] + " " + res1.DATA.FIRST_NAME[i] + " " + res1.DATA.LAST_NAME[i] + " " + "is valid." + "</span>";
                  };                  
                 $('#submitbutton').removeAttr('disabled');//now id is valid so enabling submit button                  
                } 
                else {
                  $('#submitbutton').attr('disabled', 'disabled'); //error with id so disabling button 
                  alert(" ID Is Not Valid.Please Enter Valid ID");
                  var s = "Entered  ID Is Not Valid.";                                  
                }     

                $("#results").html(s);                
                },"json");                  
           });  }); 
    </script>

<script type="text/javascript">
            $(document).ready(function() {              
            $('#2').blur(function() {              
            var id = $('#2').val();             
            $.getJSON("user.cfc?method=getRequests&returnformat=json&queryFormat=column",{"status":id}, function(res,code) {

                if(res.ROWCOUNT > 0){  

                  for(var i=0; i<res.ROWCOUNT; i++) {                   
                    s = "<span class='dPostTxt'>" + res.DATA.ID[i] + " " + res.DATA.FIRST_NAME[i] + " " + res.DATA.LAST_NAME[i] + " " + "is valid." + "</span>";
                  };
                 $('#submitbutton').removeAttr('disabled');//now id is valid so enabling submit button                  
                } 
                else {                  
                  $('#submitbutton').attr('disabled', 'disabled'); //error with id so disabling button 
                  alert(" ID Is Not Valid.Please Enter Valid ID");
                  var s = "Entered  ID Is Not Valid.";                                  
                }     

                $("#results1").html(s);                
                },"json");                  
           });  }); 
    </script>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

薄情伤 2024-11-14 14:19:53
$("form").submit(function() { 
    bool valid = goValidateForm();
    if(!valid)
        return false; //prevents form from submitting
    else
        goDoSomeCoolStuff();

});
$("form").submit(function() { 
    bool valid = goValidateForm();
    if(!valid)
        return false; //prevents form from submitting
    else
        goDoSomeCoolStuff();

});
小嗲 2024-11-14 14:19:53

将您的检查指定为在 $("form").submit() 上触发,而不是在字段的 onBlur 上触发。这样您就可以同时检查两种状态,如果您的表单值错误等,可能会向页面发出警报。

assign your checks to fire on $("form").submit() instead of onBlur of the fields. that way you can check both states at the same time, maybe fire a alert to the page if your form values are wrong &etc.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文