admin管理员组

文章数量:1323342

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. /

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

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/LX7wH/

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

Share Improve this question asked Dec 31, 2013 at 14:01 Amit T.Amit T. 832 silver badges12 bronze badges 0
Add a ment  | 

3 Answers 3

Reset to default 9

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.">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/KDZDE/

本文标签: javascriptdisable link if checkbox s uncheckedStack Overflow