为什么javascript在'return false'

why javascript working after 'return false'?

本文关键字:false return javascript 为什么      更新时间:2023-09-26

Hi我有一个用于动态添加文本框的JavaScript验证代码,当文本框有空值并且没有停止时,它会显示警报

$(document).on('click', '#med_submit', function () {
    $(".medicine_name").each(function (i) {
        var id = this.id;
        var value = $("#" + id).val();
        if (value.trim() == "") {
            alert('Medicine name should not be empty');
            return false;
        }
    });
    $(".medicine_quantity").each(function (i) {
        var id = this.id;
        var value = $("#" + id).val();
        if (value.trim() == "") {
            alert('Quantity name should not be empty');
            return false;
        }
    });
});

这是我的脚本,在medicine_name中的每个函数中都有任何一个medicine_name类文本框是空的,然后它显示警报消息,它也将转到下一个medicine_quantity每个函数,并显示该警报,这里我在警报后使用return false,然后这是如何发生的,请帮助我

只需在显示警报时设置一个标志,如果设置了标志,则提前保释。

$(document).on('click', '#med_submit', function() {
  var alertShown = false;
  $(".medicine_name").each(function(i) {
    var id = this.id;
    var value = $("#" + id).val();
    if (value.trim() == "") {
      alert('Medicine name should not be empty');
      alertShown = true;
      return false;
    }
  });
  if (alertShown) return;
  
  $(".medicine_quantity").each(function(i) {
    var id = this.id;
    var value = $("#" + id).val();
    if (value.trim() == "") {
      alert('Quantity name should not be empty');
      return false;
    }
  });
});

为什么return false;不起作用

仅仅在.each()回调函数中使用return false;是不够的,因为所做的只是停止处理jQuery集中的其余匹配元素。

来自jQuery文档:

您可以通过返回false从回调函数中停止循环。

换句话说,它所做的只是使.each()调用比正常情况下更快地完成。

不过,无论何时结束,在代码中的第一个.each()调用之后要执行的下一行代码都是第二个.each()调用。它不知道第一个.each()调用发生了什么,所以它将从第一个匹配的元素开始,并为其调用所提供的函数,然后继续处理其余匹配的元素,除非函数返回false,否则它将停止。

var blank_found = false;
$(".medicine_name").each(function (i){
   var id=this.id;
   var value=$("#"+id).val();
   if(value.trim()==""){ 
       blank_found=true;
       alert('Medicine name should not be empty');
       return false;
    }
});
if (blank_found){
   return false;
}
$(".medicine_quantity").each(function (i){
   var id=this.id;
   var value=$("#"+id).val();
   if(value.trim()==""){
      alert('Quantity name should not be empty');
      return false;
   }
 });

您可以使用这个。。

$(document).on('click','#med_submit',function(){
var isempty=false;
   $(".medicine_name").each(function (i)
                    {
                            var id=this.id;
                            var value=$("#"+id).val();
                            if(value.trim()==""){isempty=true; alert('Medicine name should not be empty'); return false;}
                    });
           $(".medicine_quantity").each(function (i)
                    {
                            var id=this.id;
                            var value=$("#"+id).val();
                            if(value.trim()==""){ isempty=true; alert('Quantity name should not be empty'); return false;}
                    });
if ( isempty==true ) {
       return false;
    }

        });