简体   繁体   English

使用jQuery启用和禁用复选框

[英]Enabling and disabling checkboxes with jQuery

basically I have a form of four HTML checkboxes. 基本上,我有四个HTML复选框的形式。 If the one with value "one" is ticked I want to disable the one with value "two" and visa versa. 如果勾选“ 1”值,我想禁用“ 2”值,反之亦然。 The rest of the checkboxes operate as normal. 其余复选框正常运行。

I've almost got it there but when I untick the box it doesn't re-enable the other again. 我几乎已经到了那里,但是当我取消选中该框时,它不会再次启用另一个。

HTML 的HTML

<form>

<input type="checkbox" name="product[]" value="one">

<input type="checkbox" name="product[]" value="two">

<input type="checkbox" name="product[]" value="three">

<input type="checkbox" name="product[]" value="four">

</form>

Script 脚本

$( "input" ).change(function() {
  var val = $(this).val();

    if(val == "one") {

     $("input[value='two']").prop( "disabled", true );

  }

  else if(val== "two") {

    $("input[value='one']").prop( "disabled", true );

  }

    });

Fiddle link 小提琴链接

This should work our for you: Fiddle Link 这应该为我们工作: Fiddle链接

 $( "input" ).change(function() { var val = $(this).val(); if(val == "one") { if(!$("input[value='two']").is('[disabled]')){ $("input[value='two']").prop( "disabled", true ); }else{ $("input[value='two']").prop( "disabled", false ); } } else if(val== "two") { if(!$("input[value='one']").is('[disabled]')){ $("input[value='one']").prop( "disabled", true ); }else{ $("input[value='one']").prop( "disabled", false ); } } }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <form> <input type="checkbox" name="product[]" value="one"> <input type="checkbox" name="product[]" value="two"> <input type="checkbox" name="product[]" value="three"> <input type="checkbox" name="product[]" value="four"> </form> 

You need to use the current state of the checkbox. 您需要使用复选框的当前状态。 To accomplish that, you can use the function $.is() along with the flag :checked 为此,您可以使用$.is()函数以及标志:checked

This alternative uses data-attribute to make it shorter. 此替代方法使用data-attribute来使其更短。

With data-attribute you're not mixed up the values and the logic to disable/enable other checkboxes. 使用data-attribute您不会混淆值和禁用/启用其他复选框的逻辑。 Therefore, the checkboxes can handle any values avoiding modifications in your disable/enable logic. 因此,复选框可以处理任何值,从而避免在禁用/启用逻辑中进行修改。

 // This way, you're separating the disable/enable logic. $("input").change(function() { var target = $(this).data('target'); if (target) $("input[data-disenable='" + target + "']").prop("disabled", $(this).is(':checked')); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <form> <input type="checkbox" name="product[]" data-disenable='1' data-target='2' value="one"> <input type="checkbox" name="product[]" data-disenable='2' data-target='1' value="two"> <input type="checkbox" name="product[]" value="three"> <input type="checkbox" name="product[]" value="four"> </form> 

You can use $(this).is(":checked") to assign on prop("disabled".. 您可以使用$(this).is(":checked")来分配prop("disabled"..

 $(function() { $('input').change(function() { var val = $(this).val(); if (val == "one") { $("input[value='two']").prop("disabled", $(this).is(":checked")); } else if (val == "two") { $("input[value='one']").prop("disabled", $(this).is(":checked")); } }); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <form> <input type="checkbox" name="product[]" value="one"> <input type="checkbox" name="product[]" value="two"> <input type="checkbox" name="product[]" value="three"> <input type="checkbox" name="product[]" value="four"> </form> 

I've almost got it there but when I untick the box it doesn't re-enable the other again. 我几乎已经到了那里,但是当我取消选中该框时,它不会再次启用另一个。

You haven't handled that case in your handler, all of your calls to prop use true as the value (eg, they all disable). 您尚未在处理程序中处理这种情况,所有对prop的调用都将true用作值(例如,它们都被禁用)。

Instead, use this.checked to use true when the current checkbox is checked, or false when it isn't: 相反,请使用this.checked在选中当前复选框时使用true ,在未选中时使用false

 $("input").change(function() { var val = $(this).val(); if (val == "one") { $("input[value='two']").prop("disabled", this.checked); } else if (val == "two") { $("input[value='one']").prop("disabled", this.checked); } }); 
 <form> <input type="checkbox" name="product[]" value="one"> <input type="checkbox" name="product[]" value="two"> <input type="checkbox" name="product[]" value="three"> <input type="checkbox" name="product[]" value="four"> </form> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> 

We can also make that a bit shorter, since the two branches do exactly the same thing, just with different selectors: 我们还可以使它更短一些,因为两个分支使用不同的选择器执行完全相同的操作:

 $("input").change(function() { var val = $(this).val(); if (val === "one" || val === "two") { var other = val == "one" ? "two" : "one"; $("input[value='" + other + "']").prop("disabled", this.checked); } }); 
 <form> <input type="checkbox" name="product[]" value="one"> <input type="checkbox" name="product[]" value="two"> <input type="checkbox" name="product[]" value="three"> <input type="checkbox" name="product[]" value="four"> </form> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> 

Or we could even do it so that the HTML defines the relationship between the checkboxes using a data-* attribute: 或者我们甚至可以这样做,以便HTML使用data-*属性定义复选框之间的关系:

 $("input").change(function() { var other = $(this).attr("data-disable"); if (other) { $("input[value='" + other + "']").prop("disabled", this.checked); } }); 
 <form> <!-- NOTE the data-disable attributes --> <input type="checkbox" name="product[]" value="one" data-disable="two"> <input type="checkbox" name="product[]" value="two" data-disable="one"> <input type="checkbox" name="product[]" value="three"> <input type="checkbox" name="product[]" value="four"> </form> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> 

(Note: You could also use data here, but there's no reason to unless you need the additional features it adds. It's not just an accessor for data-* attributes; it's both more and less than that.) (注意:您也可以在此处使用data ,但是没有理由除非您需要它添加的其他功能。它不仅data-*属性的访问器,而且数量更多,也更少。)

$( "input" ).change(function(e) {
    var val = $(this).val();
    if(val == "one") {
        $("input[value='two']").prop( "disabled", e.target.checked );
    }

    else if(val== "two") {
        $("input[value='one']").prop( "disabled", e.target.checked );
    }
});

this will do the magic 这会做魔术

pay attention: you should pass e - event argument, and check its state e.target.checked 注意:您应该传递e事件参数,并检查其状态e.target.checked

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

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