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

时间:2011-05-16 19:54:37

标签: jquery json cfc

嗨下面是我用于表单的代码,用于验证在两个输入字段中输入的id。所有代码都正常工作。如果这两个字段都无效,那么如何停止提交表单就像我一样如果记录返回为真,则禁用并启用提交按钮。但如果任何一个字段有效,则提交按钮启用并允许用户提交表单。

<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>

2 个答案:

答案 0 :(得分:1)

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

});

答案 1 :(得分:0)

将您的支票分配给$("form").submit()而不是字段的onBlur。这样你就可以同时检查这两种状态,如果你的表格值不对,可能会向页面发出警报等等。