2013-07-17 133 views
0

我有一個標籤式指數同一個頁面上有兩種形式,如jqueryui.com/tabs/#jQuery驗證的一種形式,而不是工作在其他形式的

我已經包括了js文件:

jQuery的。 validate.js,附加me​​thods.js,在的jquery.js首先TAB

形態:在所述第二TAB

<form action="#" method="post" accept-charset="utf-8" name="org" id="org" autocomplete="off">   
    <table border="0" cellpadding="4" cellspacing="0"> 
    <tr> 
     <td> <label>Name: </label></td> 
     <td><input type="text" name="head" value="" id="head" class="required" /></td> 
    </tr> 
    <tr> 
     <td><label></label></td> 
     <td><input type="submit" name="Submit" value="Add" onclick="javascript:addForm();" /></td> 
    </tr> 
    </table> 
</form> 

形式:

<form action="" method="post" accept-charset="utf-8" name="orgEdit" id="orgEdit" autocomplete="off"> 
    <table border="0" cellpadding="4" cellspacing="0"> 
    <tr> 
     <td><label>Head: </label></td> 
     <td><input type="text" name="editHead" value="" id="editHead" class="required" /></td> 
    </tr> 
    <tr> 
     <td><label></label></td> 
     <td><input type="submit" name="Submit" value="Update" onclick="javascript:editForm();" /></td> 
    </tr> 
    </table> 
</form> 

點擊的這兩種形式提交按鈕如下驗證部分,

<script> 
function addForm() { 
    $("#org").validate({ 
     rules: { 
      head: { 
       required:true, 
       letterswithbasicpunc:true 
      }, 
     }, 
     messages: { 
      head: { 
       required:"&nbsp;Please enter head name.", 
       letterswithbasicpunc:"&nbsp;Please enter only characters." 
      }, 
     }, 
     submitHandler: function(form) { 
      $.post("url",$("#org").serialize(),function(data){ 
       if(data != 'E') { 
        alert("Data Inserted Successfully."); 
        location.reload(); 
       } else 
        alert("Failed."); 
      }); 
     } 
    }); 
} 

function editForm() { 
    $("#orgEdit").validate({ 
     rules: { 
      editHead: { 
       required:true, 
       letterswithbasicpunc:true 
      }, 
     }, 
     messages: { 
      editHead: { 
       required:"&nbsp;Please enter head name.", 
       letterswithbasicpunc:"&nbsp;Please enter only characters." 
      }, 
     }, 
     submitHandler: function(form) { 
      $.post("url",$("#orgEdit").serialize(),function(data){ 
       if(data != 'E') { 
        alert("Data Inserted Successfully."); 
        location.reload(); 
       } else 
        alert("Failed."); 
      }); 
     } 
    }); 
} 
    </script> 

jQuery的驗證工作正常,同時加入了數據,但是在編輯表格失敗。 請幫助我。

+0

你是什麼意思的 「失敗」?它從來沒有正確驗證?或者驗證根本不運行? – kennypu

+0

驗證根本不運行,點擊更新按鈕後,將調用「editForm」函數,頁面正在刷新。 – saran

+0

當驗證器被調用時,如果輸入字段不可見,那麼他們將不會被驗證 –

回答

1

你應該改變的代碼如下:

<form onsubmit="return editForm(this);" action="" method="post" accept-charset="utf-8" name="orgEdit" id="orgEdit"  autocomplete="off"> 
    <table border="0" cellpadding="4" cellspacing="0"> 
    <tr> 
     <td><label>Head: </label></td> 
     <td><input type="text" name="editHead" value="" id="editHead" class="required" /></td> 
    </tr> 
    <tr> 
     <td><label></label></td> 
     <td><input type="submit" name="Submit" value="Update" /></td> 
    </tr> 
    </table> 
</form> 

的Javascript:

function editForm(form) { 
    $(form).validate({ 
     rules: { 
     editHead: { 
      required:true, 
      letterswithbasicpunc:true 
     }, 
     }, 
     messages: { 
     editHead: { 
      required:"&nbsp;Please enter head name.", 
      letterswithbasicpunc:"&nbsp;Please enter only characters." 
     }, 
     }, 
     submitHandler: function(form) { 
     $.post("url",$("#orgEdit").serialize(),function(data){ 
      if(data != 'E') { 
       alert("Data Inserted Successfully."); 
       location.reload(); 
      } else 
       alert("Failed."); 
     }); 
     } 
    }); 
    return $(form).valid(); 
    } 
+0

謝謝你的時間。 js文件似乎有衝突。 – saran