javascript - 如何使用 javascript ajax 验证每个文本框?

标签 javascript ajax jsp

我想验证每个动态文本框,这样当在每个文本框中输入输入(pid)时,输入将被传递到 ajax 中,并且如果 personName 在数据库中并且数据库中的 personName 与另一个文本框中的 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>

请帮助了解如何验证每个文本框?谢谢

最佳答案

您可以在每个文本框中迭代循环,并可以根据数据库值检查和验证文本框的值

var personName = data.pName; // this is from back-end 

    $(".personName").each(function(){
      if($(this).val() == personName ){

       }else{

       }
    })

关于javascript - 如何使用 javascript ajax 验证每个文本框?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40610054/

相关文章:

javascript - jquery如何使用子元素id获取父元素的类?

jQuery - 如何通过 Ajax 放置 JSON?

jquery - html 文本覆盖自身

apache - 内网无法访问虚拟主机

java - 如果在后台运行的 Spring Batch 作业将更新 sql 结果状态,如何更新 jsp 状态(从提交到处理然后成功)

java.lang.NoClassDefFoundError : org/skife/jdbi/v2/DBI 错误

javascript - 在 URL 浏览器字段中输入子 URL 时不显示路由

javascript - div 位置不会在响应式页面中重置

javascript - 无法从 Firebase Storage UploadTask 更新进度栏

php - 在 Laravel Ajax 请求验证中获取错误字段名称