简体   繁体   English

如何在jQuery中互斥的复选框上实现click事件?

[英]How to implement a click event on mutually exclusive checkboxes in jQuery?

I have two checkboxes: 我有两个复选框:

<input id="outside" type="checkbox" value="1" data-gid="41820122" />
<input id="inside" type="checkbox" value="1" data-gid="41820122" />

I've made them mutually exclusive with: 我通过以下方式使它们互斥:

$(function(){
  //mutually exclusive checkboxes
  $("input[data-gid]").click(function(){
    var gid = $(this).attr('data-gid');
    var fid = $(this).attr('id');
    var checkboxes = $("input[data-gid=" + gid + "][id!=" + fid + "]");
    checkboxes.attr("checked", false);
   });
});

This works fine. 这很好。 But I also want to add additional click functionality to the 'inside' checkbox. 但我也想在“内部”复选框中添加其他点击功能。 I want it to enable/disable a textarea on the same form, so I've done this: 我希望它以相同的形式启用/禁用文本区域,所以我这样做了:

$('#application_inside').click(function() {
        if ($(this).is(':checked')) {
          return $('textarea').removeAttr('disabled');
        } else {
          return $('textarea').attr('disabled', 'disabled');
        }
 });

So, if the 'inside' checkbox is checked, the textarea will be enabled, if it's not checked, the textarea should be disabled. 因此,如果选中“内部”复选框,则将启用文本区域;如果未选中,则应禁用文本区域。

The problem is that if the 'inside' checkbox is checked, and the user then checks the 'outside' checkbox, the 'inside' becomes unchecked (as it should be), but the textarea remains enabled. 问题在于,如果选中了“内部”复选框,然后用户选中了“外部”复选框,则“内部”将变为未选中状态(应该如此),但文本区域保持启用状态。 This appears to be because the 'inside' checkbox was never actually clicked by the user. 这似乎是因为用户从未真正单击过“内部”复选框。

I've tried working around this with the following: 我尝试使用以下方法解决此问题:

$(function(){
  //mutually exclusive checkboxes
  $("input[data-gid]").click(function(){
    var gid = $(this).attr('data-gid');
    var fid = $(this).attr('id');
    var checkboxes = $("input[data-gid=" + gid + "][id!=" + fid + "]");
    checkboxes.attr("checked", false);
    checkboxes.triggerHandler("click"); //new code to fire any click code associated with unchecked boxes
   });
});

But this just throws the browser in a loop, since the two different click events end up calling each other. 但这只会使浏览器陷入循环,因为两个不同的单击事件最终会相互调用。

How can I keep my mutually exclusive code while still allowing the enable/disable code for the 'inside' checkbox to work? 如何在保持互斥代码的同时仍允许“内部”复选框的启用/禁用代码正常工作?

You can create a custom event that you trigger when you click on #application_inside . 您可以创建一个自定义事件 ,该事件在您单击 #application_inside时触发。 Also you will fire this custom event when you uncheck other boxes because of the exclusiveness. 另外,由于排他性,当您取消选中其他复选框时,也会触发此自定义事件

Here is an example: http://jsfiddle.net/gs78t/2/ 这是一个示例: http : //jsfiddle.net/gs78t/2/

you may try something like this 你可以尝试这样的事情

var insideChanged = function() {
    if ($('#inside').is(':checked')) {
      return $('textarea').removeAttr('disabled');
    } else {
      return $('textarea').attr('disabled', 'disabled');
    }
};

$('#application_inside').click(insideChanged);

$(function(){
  //mutually exclusive checkboxes
  $("input[data-gid]").click(function(){
   var gid = $(this).attr('data-gid');
    var fid = $(this).attr('id');
    var checkboxes = $("input[data-gid=" + gid + "][id!=" + fid + "]");
    checkboxes.attr("checked", false);
    insideChanged(); //new code to fire any click code associated with unchecked boxes
   });
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM