我有一个表单,其中我有一个地址字段,用户可以在其中输入多个地址。这取决于用户。他们可以输入很多地址,工作正常。
我正在检查用户没有两次输入相同的地址。为此我写了一些jQuery代码。
jQuery('#EnrollmentEnrollUserForm').submit(function(){
// alert(jQuery('#EnrollmentServiceAddress1').val());
// alert(jQuery('.premise-body').html());
var add=jQuery('#EnrollmentServiceAddress1').val();
add=add.toLowerCase();
var address_string =jQuery('table.premise_details').find('tr:first td:last').html();
// alert(address_string);
var address = address_string.split('::');
address[0]=address[0].toLowerCase();
// alert(address[0]);
if(add==address[0]){
alert("These premises already exist. Please enter other premises.");
return false;
}
// checking whether a premise added is same as the added premises or not
// var premise_check = true;
var retval = true;
jQuery('table.premise_details').each(function() {
var address_string = jQuery(this).find('tr:first td:last').html();
// alert(jQuery(this).find('tr:first td:last').html());
var address = address_string.split('::');
address[0]=address[0].toLowerCase();
// alert(address[0]);
if(add==(address[0])){
alert("These premises were already added... Please enter other premises.");
retval = false;
}
});
if(!retval){
return false;
}
});
此代码有时仅适用。我面临的问题是,如果用户再次输入相同的地址,它会显示警告,但页面正在无限循环加载。
有谁可以告诉我这是什么问题。
感谢您的帮助!
答案 0 :(得分:1)
jQuery('#EnrollmentEnrollUserForm').submit(function(){
var add=jQuery('#EnrollmentServiceAddress1').val();
add=add.toLowerCase();
var address_string =jQuery('table.premise_details').find('tr:first td:last').html();
var address = address_string.split('::');
address[0]=address[0].toLowerCase();
if(add==address[0]){
alert("These premises already exist. Please enter other premises.");
return false;
}
jQuery('table.premise_details').each(function() {
var address_string = jQuery(this).find('tr:first td:last').html();
var address = address_string.split('::');
address[0]=address[0].toLowerCase();
if(add==(address[0])){
alert("These premises were already added... Please enter other premises.");
return false;
}
});
});
你遇到问题的原因是你在完成每个功能的循环后调用你的retval,显示它的最后设定值为retval并为你创建问题,这里我有更改代码并返回false为我符合我的条件。 我编辑了代码..请检查此代码
答案 1 :(得分:0)
现在工作正常..
jQuery('#EnrollmentEnrollUserForm').submit(function(){
var add=jQuery('#EnrollmentServiceAddress1').val();
add=add.toLowerCase();
// checking whether a premise added is same as the added premise or not
jQuery('table.premise_details').each(function() {
var address_string = jQuery(this).find('tr:first td:last').html();
var address = address_string.split('::');
address[0]=address[0].toLowerCase();
if(add==(address[0])){
alert("This premise is already added... Please enter other premise.");
return false;
}
});
});