我想验证每个动态文本框,以便在每个文本框中输入输入(pid)时,输入将被传递到ajax,如果personName在DB中,并且DB中的personName与之相同,则返回true另一个文本框中的personName。但我不确定如何做到这一点。这是我的样本检查。
使用Javascript:
function validateTextbox() {
var pName = document.getElementsByClassName("personNameStatic");
var pNameVal = pName[0].value;//value of personName in another textbox
var pn = document.getElementsByClassName("personName"); //person name which will be passed to an ajax to get personName from DB
for (var i = 0; i < pn.length; i++) {
var pnval = pn[i].value;
$.ajax({
//ajax here
success: function(data) {
var personName = data.pName;
if (pNameVal == personName){ //compare pNameVal (from another textbox) to personName (coming from DB)
alert("Name is valid.");
}else{
alert ("Name is invalid");
}
},//END: success
});//END: ajax
}
}
HTML:
<td><input type="text" name="personName" id="personId-1" class="personName" size="30"/></td> //textbox which inputs the pid
<td><input type="text" name="personName" id="personId-2" class="personName" size="30"/></td> //textbox which inputs the pid
<td><input type="text" name="personName" id="personId-3" class="personName" size="30"/></td> //textbox which inputs the pid
<td><input type='text' id='personIdstatic' class='personNameStatic' size='30' value='John' disabled /></td>
请帮助了解如何验证每个文本框?感谢
答案 0 :(得分:0)
您可以在每个文本框中迭代循环,并可以检查&amp;验证文本框的值为db值
var personName = data.pName; // this is from back-end
$(".personName").each(function(){
if($(this).val() == personName ){
}else{
}
})