2010-10-22 128 views
0

我有一個帶有選擇字段A的表單。該字段可以根據URL動態填充,也可以像往常一樣選擇。驗證AJAX生成的表單字段

一旦在字段A中選擇了一個值,可以選擇字段B並使用JQuery AJAX進行公開。

這是問題所在。如果字段A保持不變,並且由URL動態填充,字段B將正確驗證。但是,如果字段A發生更改,字段B將不再嘗試驗證。

字段A

<select name="FieldA" id="FieldA"> 
<option value="">Please Select</option> 
<?php 
    while($FieldA= mysql_fetch_array($result2)) { 
?> 
<option value="<?php echo $FieldA['FieldAID']; ?>"<?php if ($var == $FieldA['FieldAID']) echo " selected=\"selected\""; ?>><?php echo $FieldA['FieldAName']; ?> </option> 
<?php } ?> 
</select> 

字段B

<select name="FieldB" id="FieldB"> 
<option value="">Please Select</option> 
<?php 
    while($FieldB = mysql_fetch_array($result)) { 
?> 
<option value="<?php echo $FieldB['FieldBID']; ?>"><?php echo str_replace('|',' - ',$FieldB['FieldBName']); ?></option> 
<?php } ?> 
</select> 

驗證標準

<script language="JavaScript" type="text/javascript"> 
var frmvalidator = new Validator("FormName"); 
frmvalidator.addValidation("FieldA","req","Please select FieldA."); 
frmvalidator.addValidation("FieldB","req","Please select FieldB."); 
</script> 

一切除了AJAX調用破壞字段B的驗證之外。如果字段B沒有被重新填充,它可以正常工作。字段B由一個包含文件構建,因此無論是由頁面還是AJAX調用填充都是相同的。

謝謝!

+0

所以......你能告訴我們從AJAX調用中獲得的確切值嗎? – treeface 2010-10-22 21:53:22

+1

你是如何最終解決的? – netadictos 2010-10-29 09:13:24

+0

我還沒有解決它。 – 2010-11-01 13:37:14

回答

0

我想你正在使用這個JS庫: http://www.javascript-coder.com/html-form/javascript-form-validation.phtml

如果它給你很多問題,也許是時候改變到一個更強大的驗證庫。

我建議這樣的:

http://bassistance.de/jquery-plugins/jquery-plugin-validation/

出現在jQuery的網頁,這是非常完整的。語法或多或少是這樣的:

$("#signupForm").validate({ 
     rules: { 
      firstname: "required", 
      lastname: "required", 
      username: { 
       required: true, 
       minlength: 2 
      }, 
      password: { 
       required: true, 
       minlength: 5 
      }, 
      confirm_password: { 
       required: true, 
       minlength: 5, 
       equalTo: "#password" 
      }, 
      email: { 
       required: true, 
       email: true 
      }, 
      topic: { 
       required: "#newsletter:checked", 
       minlength: 2 
      }, 
      agree: "required" 
     }, 
     messages: { 
      firstname: "Please enter your firstname", 
      lastname: "Please enter your lastname", 
      username: { 
       required: "Please enter a username", 
       minlength: "Your username must consist of at least 2 characters" 
      }, 
      password: { 
       required: "Please provide a password", 
       minlength: "Your password must be at least 5 characters long" 
      }, 
      confirm_password: { 
       required: "Please provide a password", 
       minlength: "Your password must be at least 5 characters long", 
       equalTo: "Please enter the same password as above" 
      }, 
      email: "Please enter a valid email address", 
      agree: "Please accept our policy" 
     } 
    }); 
+0

我將嘗試更新表單以使用JQuery驗證,然後報告它是否有效。我有一個同事,雖然有了更新的驗證,但他仍然有同樣的問題。由AJAX生成的字段不驗證,但正確發佈信息。謝謝! – 2010-10-26 13:06:46