JQuery using one checkbox to uncheck another programatically

和自甴很熟 提交于 2019-12-23 20:00:41

问题


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/



来源:https://stackoverflow.com/questions/4817735/jquery-using-one-checkbox-to-uncheck-another-programatically

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!