为什么javascript在'return false'
why javascript working after 'return false'?
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;
}
});
相关文章:
- 为什么javascript在'return false'
- return false不阻止点击
- return false不停止表单提交
- 表单提交没有'调用return false后无法工作
- svgpanzoom:beforeZoom on return false dos'不要停下
- 为什么event.prventDefault()和'return false'停止我的AJAX表单提交操
- 我可以在onclick=return false的链接上触发点击事件吗
- return false dos't禁止提交表格
- JavaScript onkeydown, return true,false
- 提交之间的差异=“;return false”;并且onsubmit=“;javascript:return false
- 为什么preventDefault不起作用,而return false起作用
- javascript doesn't return false
- JavaScript: onclick and return false
- Ajax success: {return false;}
- 函数() { return false; } 的操作细节().
- 只写一次“return false”来处理点击许多链接
- onsubmit=“return false”是什么意思?(JavaScript,jQuery)
- 还有没有'return false'Javascript短路
- Return false不适用于AJAX
- Return false对第二个onclick切换事件无效