How to escape from for … each loop and method at the same time?

 ̄綄美尐妖づ 提交于 2019-12-12 03:57:15

问题


I am using the code below, that iterating over select options. I checked if options value has already entered. It escapes from for-each but it doesn't exit from method.

yeniIlacBagla_ilacBagla: function(){
    $("#bagliIlaclar > option").each(function(){
        if(this.value===$("#baglanacakIlacID").val()){
            alert($("#baglanacakIlacAdi").val()+'\n adlı ilaç zaten bağlıdır!'); 
            return;
        }else{
            $("#bagliIlaclar").append($('<option></option>').val($("#baglanacakIlacID").val()).html($("#baglanacakIlacAdi").val()));
        }
    });
}

回答1:


According to the documentation of jquery I found the following

We can break the $.each() loop at a particular iteration by making the callback function return false. Returning non-false is the same as a continue statement in a for loop; it will skip immediately to the next iteration.




回答2:


From the documentation at http://api.jquery.com/each/ :

You can stop the loop from within the callback function by returning false.



来源:https://stackoverflow.com/questions/15574121/how-to-escape-from-for-each-loop-and-method-at-the-same-time

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!