JQuery using one checkbox to uncheck another progr

2019-07-06 05:36发布

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

3条回答
Luminary・发光体
2楼-- · 2019-07-06 05:55

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

查看更多
欢心
3楼-- · 2019-07-06 06:13
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/

查看更多
Root(大扎)
4楼-- · 2019-07-06 06:16

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

查看更多
登录 后发表回答