javascript - 添加具有相同类的新字段后验证输入字段
问题描述
我有一个输入字段和“添加”按钮来存储。单击“添加”按钮时,应创建另一个具有相同类名的输入字段。但在创建之前有一个验证检查。但验证只是第一次。第二次没有验证检查。
下面是我的代码。
$('#add').click(function (e) {
e.preventDefault();
if ($('.js-user-email').val()) {
debugger;
$("#divSpecificToUserError span").text("");
$('#divSpecificToUserError').addClass("d-none");
if (ValidateEmailformat($('.js-user-email').val())) {
//debugger;
$.ajax({
type: "POST",
url: "Stepup.aspx/Exists",
data: JSON.stringify({ emailId: $('.js-user-email').val() }),
cache: false,
dataType: "json",
contentType: "application/json; charset=utf-8",
success: function (response) {
debugger;
if (response != null && response.d != null) {
var data = response.d;
if (data > 0) {
addDynamicTextboxes();
}
else {
$("#divSpecificToUserError span").text("not a valid user");
$('#divSpecificToUserError').removeClass("d-none");
}
}
},
failure: function (response) {
alert(response.d);
}
});
}
else {
$("#divSpecificToUserError span").text("Please enter email id in valid format");
$('#divSpecificToUserError').removeClass("d-none");
}
}
else {
$("#divSpecificToUserError span").text("No E-mail has been entered ");
$('#divSpecificToUserError').removeClass("d-none");
}
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="field form-group form-group-padding">
<label class="col-form-label">User E-mail(s) to Notify:</label>
<input type="text" id="field1" name="userEmail1" class="input form-control js-user-email" placeholder="example@domain.com" autofocus />
<button id="add" class="btn-sm btn-secondary add-more-specific pull-right btn-user-email" title="Add" type="button">+</button>
<div class="col-md-3 d-none alert-danger" id="divSpecificToUserError" style="margin-top: 6px">
<span></span>
</div>
</div>
解决方案
哦,我明白你的问题是什么了。
// 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 ...
});
});
}
推荐阅读
- python-3.x - AttributeError:“模块”对象没有属性“完全匹配”
- cmake - 使用具有特定配置的 target_link_libraries() 会导致链接问题
- oracle - Oracle 18c XE 数据库无法连接 - IP 地址动态更改 - 错误 17002
- sqlite - 将日期字符串 dd/mm/yyyy 格式化为 yyyy-mm-dd
- bitbucket - How to use 2-factor auth with Bitbucket API
- php - What is the name of "::" in PHP?
- rust - 假设两个可变引用不能别名,为什么 Rust 编译器不优化代码?
- regex - 带有正则表达式的 grep-bash 命令返回 0 个结果,正在寻找替代方案
- javascript - Auth0 忽略范围选项
- angular - Calling ngZone.runTask in ngZone.runOutsideAngular still triggering change detection in other components