简体   繁体   中英

disable link if checkbox s unchecked

I want to disable link if user uncheck the checkbox.

tried something like:

$('#check1').click(function() {
  if(!$(this).is(':checked')){
    $('#tet1').bind('click', function(){ return false; });
  }else{
    $('#tet1').unbind('click');
  }
});

but it did not work. http://jsfiddle.net/LX7wH/

Why this is not working for me? where i'm wrong? Thank you!

You don't need to do anything when the checkbox is changed, just check if it's checked within an event handler for the anchor, and if it is checked, prevent the default action.

$('#tet1').on('click', function(e) {
    if ( $('#check1').is(':checked') ) e.preventDefault();
});

Try,

var xEnabled = true;

$('#check1').click(function() {
 xEnabled = $(this).is(':checked');
});

$('#tet1').click(function(){
   if(!xEnabled) { return false; }
})

DEMO

I believe this is what you are after:

<a href="http://www.google.com">Google</a>
<input type="checkbox" id="checkBox" />

$("a").click(function(e) {
        if($("#checkBox").prop("checked") === true) {
            e.preventDefault();
            return false;
        } else {
            return true;
        };
    });

This will stop all links working however you can change the "a" selector to what ever you want.

http://jsfiddle.net/KDZDE/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM