我正在開發一個Web應用程序,其中使用AJAX動態生成/加載相同的HTML表單。如何以多種形式獲取同名的字段的值?
下面是示例形式(幾乎20形成在某時刻加載):
<div class="replybox" id="rbox1">
<cite>
<form action="" method="post" id="replypostform">
<p>
<textarea id="replytxt" name="replytxt" rows="2" cols="43" class="replytextarea"></textarea>
<input class="button" type="submit" value="Reply"/>
</p>
</form>
</cite>
</div>
,我使用的jQuery驗證插件來驗證它們。表單驗證工作正常,但現在我只需要在提交時獲取textarea
的內容,如果我只是使用.val()
函數,那麼它將只返回第一個表單的textarea
內容。
但我想要點擊用戶點擊的textarea
的內容。
這裏是我的jQuery驗證碼:
jQuery("#replypostform").live('mouseover', function(){
jQuery(this).validate({
errorElement:'div',
rules: {
replytxt:{
required: true
}
},
messages: {
replytxt:{
required: "<div style='color:red'>Please write a reply for posting it.</div>"
}
},
submitHandler:function(mform){
//here I want get the content of textarea
}
});
});
在這裏,我只是做了與鼠標懸停事件一招,以驗證具有相同名稱/ ID和相同的字段中的多個表單。
我的頁面使用AJAX加載大約20個完全相同的表單。
所以建議我如何獲得特定形式的textarea
的內容。
-Thanks
一個ID只能在HTML文檔中顯示一次。如果您多次使用此表格,則應使用類別進行標識而不是標識。 – Andrew