JQuery using one checkbox to uncheck another progr

2019-07-06 05:33发布

问题:

I'm after a script where one checkbox will uncheck another. It is a bit complicated though as all the data is loaded programatically. So if a checkbox is unchecked the procedure is to take its src value, then go through the other inputs and find inputs that have a title of 'RQlevel' + the src value of the clicked elements and set it to unchecked.

Here is the current code.

function levels() {
  var test = $(this).attr('src'); 
  if ($(this).is(':not(:checked)')) { 
    $(':input').each(function() { 
      if ($(this).attr('title') === ('RQLevel' + test)) {
        $(this).removeAttr('checked');
      }
    });
  }
}

There is a working example here that will illustrate the issue http://jsfiddle.net/V8FeW/7/ If both boxes are checked and the first box is then unchecked it should take the second box with it.

Marvellous

回答1:

function levels() {
  var test = $(this).attr('src'); 
  if (!this.checked)
     $('input:checkbox[title=RQlevel' + test + ']').removeAttr('checked');
}

$(function() {
    $('input:checkbox').bind('change', levels);
});

Demo: http://jsfiddle.net/V8FeW/12/



回答2:

$('input [title="RQlevel"]').attr('checked', $(this).attr('checked'));



回答3:

Here you can find a nice, short and elegant solution: http://briancray.com/2009/08/06/check-all-jquery-javascript/