-2

我有一个表单,其中有一个地址字段,用户可以在其中输入许多地址。这取决于用户。他们可以输入许多地址,它工作正常。

我正在检查用户是否两次输入相同的地址。为此,我编写了一些 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;
  }
});

此代码仅在某些时候有效。我面临的问题是,如果用户再次输入相同的地址,它会显示警报,但页面正在无限循环中加载。

谁能告诉我这有什么问题。

谢谢您的帮助!

4

2 回答 2

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条件。我已经编辑了代码..请检查此代码

于 2012-05-25T06:00:20.780 回答
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;
                        }
                });
    });
于 2012-05-25T07:47:33.867 回答