JQuery选中一个复选框,然后取消选中/禁用具有相同名称属性的其他复选框

时间:2018-06-09 15:15:06

标签: javascript jquery

我有三个输入复选框,它们都具有相同的名称属性我希望其中一个被检查然后其他取消选中和禁用。我想在函数调用函数和更改事件中的jquery代码,如下面的代码或任何其他方式正常工作。

<div class="topmenuitems">
    <input type="checkbox" name="menu-item-topitemtypes" value="itemwithouticon" />
    <input type="checkbox" name="menu-item-topitemtypes" value="itemwithicon" />
    <input type="checkbox" name="menu-item-topitemtypes" value="itemicon" />
</div>

var itemWithIconCheckbox = $('.topmenitems input');
var topItemTypesFunc = function() {
    //Jquery code here
};
topItemTypesFunc();
topItemTypeCheckboxes.on( 'change', topItemTypesFunc);

2 个答案:

答案 0 :(得分:1)

您可以通过单选按钮执行所需的功能。在这里,我在这里实现了一个简单的逻辑。当一个复选框被更改时,取消选中复选框,然后单击所选复选框。这是一个有效的例子。

import os.path
if not os.path.exists(file_path):
   #run start up script
   file = open (same_name_as_file_path, "w") #creates our file to say startup is complete you could write to this if you wanted as well
   file.close
$('input[name="menu-item-topitemtypes"]').on( 'change', function(){
  if($(this).prop('checked')){
    $('input[name="menu-item-topitemtypes"]').not($(this)).prop('checked', false);
  }
});

答案 1 :(得分:1)

这可能对您有所帮助

  $('input[type="checkbox"]').on('change', function(){
      if($(this).is(':checked')){
        $(this).siblings().attr('disabled', 'true');
      }else{
        $(this).siblings().removeAttr('disabled', 'false');

      }
  })