javascript - jQuery - trying to do a function inside an if statement - Stack Overflow

I'm trying to add some validation to a form.I have a jQuery function that is doing exactly what

I'm trying to add some validation to a form. I have a jQuery function that is doing exactly what I want:

      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.

I'm trying to add some validation to a form. I have a jQuery function that is doing exactly what I want:

      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.

Share Improve this question asked Jan 31, 2013 at 23:57 Morgan KayMorgan Kay 3082 gold badges3 silver badges19 bronze badges 0
Add a ment  | 

3 Answers 3

Reset to default 1

What does firebug or Chrome console tell you? You could try something like this:

$('#top').is(':checked')

as in (thanks 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;
});

try

$('#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

  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;

        });

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.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745461740a4628738.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信