开发者

Is it possible to stop a jQuery function with a checkbox (on/off)?

开发者 https://www.devze.com 2023-01-13 17:55 出处:网络
Is it possible to stop a jQuery开发者_StackOverflow function with a checkbox (on/off)?Any examples out there?Try this:

Is it possible to stop a jQuery开发者_StackOverflow function with a checkbox (on/off)? Any examples out there?


Try this:

http://jsfiddle.net/nNREP/1/

There's no magic to this...you're simply testing in the script if your checkbox is checked. If you would also like to cancel termination of, for example, a link when it's clicked (the link's click event) or a form's submission (the form's submit event), simply return false for those specific events like this:

$('#myForm').submit(function(e) {
    if (!$('#mycheck').is(":checked")) {
        return false;
    } else {
        //do something...or nothing...whatever...
    }
});

Edit: Your tiptip plugin doesn't appear to have an option to conditionally terminate the script. You can get around this by doing one of two things....adding a few conditional lines in the plugin or automatically disabling the plugin when the checkbox is unchecked (this is probably going to be comparatively CPU intensive). So let's focus on the first option...

When you set up your tiptip plugin, put in something like this:

$(".someClass").tipTip({maxWidth: "auto", edgeOffset: 10, enter: myFunction});

That myFunction will be called at the very start of the "active_tiptip", before anything else happens. Go to line 101 of the plugin's source code and you can see how it's being called. I found this by searching the document for the word "enter"...I found two instances...one when the property is originally being established with an empty function on line 35 and one at the beginning of the active_tiptip() function. Now, if you want to be able to use that myFunction to either cancel or not cancel the execution of the script, change line 101 in the tiptip.js from this:

opts.enter.call(this);

To this:

if (!opts.enter.call(this)) {
    return false;
}

Then create your myFunction that does this:

function myFunction() {
    return $('#mycheck').is(":checked");
}

And so, myFunction will return true when checked and false when not. If false, it will halt the execution of the active_tiptip() function, thereby making sure your tooltip doens't pop up.

That's about as deep as I'm willing to go for this. The rest you can figure out for yourself.


Something like so:

function something () {
     if ( $("my.checkbox:checked").size() > 0 ) {
          // do something
     }else{
          // do nothing
     }
}

Something along those lines anyway.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号