Javascript 添加具有相同类的新字段后对输入字段进行验证

Javascript 添加具有相同类的新字段后对输入字段进行验证,javascript,jquery,asp.net,ajax,Javascript,Jquery,Asp.net,Ajax,我有一个输入字段和“添加”按钮来存储。单击“添加”按钮时,应创建另一个具有相同类名的输入字段。但在创建之前,需要进行验证检查。但验证只是第一次。第二次没有验证检查 下面是我的代码 $('#添加')。单击(函数(e){ e、 预防默认值(); if($('.js用户电子邮件').val(){ 调试器; $(“#divspecifictouserror span”).text(“”); $('#divSpecificToUserError').addClass(“d-none”); if(Vali

我有一个输入字段和“添加”按钮来存储。单击“添加”按钮时,应创建另一个具有相同类名的输入字段。但在创建之前,需要进行验证检查。但验证只是第一次。第二次没有验证检查

下面是我的代码

$('#添加')。单击(函数(e){
e、 预防默认值();
if($('.js用户电子邮件').val(){
调试器;
$(“#divspecifictouserror span”).text(“”);
$('#divSpecificToUserError').addClass(“d-none”);
if(ValidateEmailformat($('.js用户电子邮件').val()){
//调试器;
$.ajax({
类型:“POST”,
url:“stepp.aspx/Exists”,
数据:JSON.stringify({emailId:$('.js user email').val()}),
cache:false,
数据类型:“json”,
contentType:“应用程序/json;字符集=utf-8”,
成功:功能(响应){
调试器;
if(response!=null&&response.d!=null){
var数据=响应d;
如果(数据>0){
添加dynamictextboxs();
}
否则{
$(“#divspecifictUserError span”).text(“非有效用户”);
$(“#divSpecificToUserError”).removeClass(“d-none”);
}
}
},
故障:功能(响应){
警报(response.d);
}
});
}
否则{
$(“#divspecifictouserror span”).text(“请以有效格式输入电子邮件id”);
$(“#divSpecificToUserError”).removeClass(“d-none”);
}
}
否则{
$(“#divspecifictouserror span”).text(“未输入任何电子邮件”);
$(“#divSpecificToUserError”).removeClass(“d-none”);
}
});

要通知的用户电子邮件:
+

$('.btn用户电子邮件')
如果这是多个元素,则需要循环它并将单击事件添加到jQuery列表中的所有元素。

$('.btn用户电子邮件')
如果这是多个元素,则需要循环它并将单击事件添加到jQuery列表中的所有元素。

哦,我明白你的问题了

// Your Add Button Function
// to insert another input here

function add(){

  // Your Add Button Code Here

  refreshValidate();
}

// Validate Function
function refreshValidate(){
   $('.btn-user-email').each(function() {
       $(this).unbind();
       $(this).click(function (e) {
           e.preventDefault();

           // Your Validate Code HERE ...
       });
   });
}

哦,我知道你有什么问题了

// Your Add Button Function
// to insert another input here

function add(){

  // Your Add Button Code Here

  refreshValidate();
}

// Validate Function
function refreshValidate(){
   $('.btn-user-email').each(function() {
       $(this).unbind();
       $(this).click(function (e) {
           e.preventDefault();

           // Your Validate Code HERE ...
       });
   });
}


添加html页面,这样我们就可以很容易地识别问题。添加html@Chandukomatoide这能回答你的问题吗?添加单击处理程序时,
$(“.class”)。单击(
它只适用于当时存在的具有该类的元素。当您创建一个新的
时,为什么不使用click event id base。@ShijoJadd html页面,因此,我们可以很容易地识别问题。添加了html@Chandukomatoid,这可以回答您的问题吗?当您添加click handler
$(“.class”)。单击(
它只适用于当时存在的具有该类的元素。当您创建新的
时,为什么不使用click event id base。@ShijoJCan您可以告诉我您希望如何循环吗?不,您不可以-jquery为您已经在集合中的元素执行此操作。您可以告诉我您希望如何循环吗?不,您不可以-jquery执行此操作在集合中为您设置。在何处调用添加函数?单击“添加”按钮时将调用添加函数。在何处调用添加函数?单击“添加”按钮时将调用添加函数。