javascript jQuery - 尝试在 if 语句中执行函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14637380/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
jQuery - trying to do a function inside an if statement
提问by Morgan Kay
I'm trying to add some validation to a form. I have a jQuery function that is doing exactly what I want:
我正在尝试向表单添加一些验证。我有一个 jQuery 函数,它完全符合我的要求:
jQuery('#post').submit(function() {
if (jQuery("#set-post-thumbnail").find('img').size() > 0) {
jQuery('#ajax-loading').hide();
jQuery('#publish').removeClass('button-primary-disabled');
return true;
}else{
alert("Please set a Featured Image!");
jQuery('#ajax-loading').hide();
jQuery('#publish').addClass('button-primary-disabled');
return false;
}
return false;
});
However, I want to change it so that this function only runs if a radio button elsewhere on the page is selected. So I tried this:
但是,我想更改它,以便此功能仅在选择页面上其他位置的单选按钮时运行。所以我试过这个:
if (jQuery('#top').checked) {
jQuery('#post').submit(function() {
if (jQuery("#set-post-thumbnail").find('img').size() > 0) {
jQuery('#ajax-loading').hide();
jQuery('#publish').removeClass('button-primary-disabled');
return true;
}else{
alert("Please set a Featured Image!");
jQuery('#ajax-loading').hide();
jQuery('#publish').addClass('button-primary-disabled');
return false;
}
return false;
});
}
That doesn't work - the function doesn't get called even if #top is checked. Can anyone explain why? I'm used to PHP, and JavaScript often throws curveballs at me.
这不起作用 - 即使选中 #top 也不会调用该函数。谁能解释为什么?我习惯了 PHP,而 JavaScript 经常向我抛出曲线球。
采纳答案by greener
What does firebug or Chrome console tell you? You could try something like this:
firebug 或 Chrome 控制台告诉您什么?你可以尝试这样的事情:
$('#top').is(':checked')
as in (thanks RET):
如(感谢 RET):
jQuery('#post').submit(function() {
if ($('#top').is(':checked')) {
if (jQuery("#set-post-thumbnail").find('img').size() > 0) {
jQuery('#ajax-loading').hide();
jQuery('#publish').removeClass('button-primary-disabled');
return true;
}else{
alert("Please set a Featured Image!");
jQuery('#ajax-loading').hide();
jQuery('#publish').addClass('button-primary-disabled');
return false;
}
}
return false;
});
回答by mons droid
try
尝试
$('#top').is(':checked')
$('#top').is(':checked')
but the function submit only binds the function and calls it every time submit is clicked. so you must put the checked check in the submit function
但是submit函数只绑定了这个函数,每次点击submit时都会调用它。所以你必须把选中的检查放在提交功能中
jQuery('#post').submit(function() {
if(!$('top').is(':checked')){ return };
if (jQuery("#set-post-thumbnail").find('img').size() > 0) {
jQuery('#ajax-loading').hide();
jQuery('#publish').removeClass('button-primary-disabled');
return true;
}
alert("Please set a Featured Image!");
jQuery('#ajax-loading').hide();
jQuery('#publish').addClass('button-primary-disabled');
return false;
});
回答by RET
Yeah, that logic won't quite do what you're hoping for. Try something like:
是的,这种逻辑不会完全符合您的期望。尝试类似:
jQuery('#post').submit(function() {
if ($('#top').is(':checked')) {
// all your existing code
I could be wrong, but I don't think the answer given by @greener is going to work, because that will only declare the submit function if #top
is checked at page create time.
我可能是错的,但我认为@greener 给出的答案不会起作用,因为如果#top
在页面创建时检查,它只会声明提交函数。