我有一个克隆文本框的场景。然后对于每个框输入,我针对 db(通过 ajax 调用)验证输入是否正确。如果输入无效,我会显示一个错误框,最后我有一个 jquery 验证器方法,该方法检查是否有任何框有错误消息,然后它会引发错误并且表单不会发布。但问题是验证器显示所有文本框的错误消息,无论值是对还是错。
<table id="next_module" class="table">
<thead>
<th>Message Id</th>
</thead>
<tbody>
<tr id="row_0">
<td>
<input id="messge-id_0" class="valid_next_messge_module numeric messgeing_id required" name="messge-id_0" type="text" onblur="get_messge_detail($(this));" value="">
<label id="txterr_0" class="next_messge_error" style="display: none;color:red;width:0px">*</label></td>
</tr>
</tbody>
</table>
<script>
var $underscore = $("#next_module tbody tr:first");
var $clone = $underscore.clone();
$clone.attr('id', $clone.attr("id") + i );
$clone
.find("input,label")
.each(function() {
$(this).attr({
id: this.id + i,
name: this.name + i,
value:'',
src:''
});
});
$clone
.appendTo("#next_module")
.show();
i++;
</script>
<script>
function get_messge_detail(obj)
{
var messge_id = $.trim(obj.val());
var id_num = (obj.attr('id')).split('_')[1];
if(messge_id)
{
var url = "/server/messge/ajax_get_messge_detail/";
var data = {'messge_id':messge_id};
$.ajaxSetup({async:false});
$.post(url, data, function (response) {
if (response["msg"])
{
no error found
}
else
{
$("#"+(obj.attr('id'))).focus();
$("#txterr_"+ id_num).show();
}
});
}
jQuery.validator.addMethod("valid_next_messge_module", function() {
var status = false;
if (parseInt( $('.next_messge_error:visible').length) > 0)
return false;
else
return true;
}, "Please enter valid next messgeing module id.");
</script>