根据其值和所选选项禁用/启用复选框

时间:2022-06-25 19:43:11

I need to hardcode those of the values of the checkboxes which are enabled/disabled, depending on the selected option value. I was trying to do something like this:

我需要对启用/禁用的复选框的值进行硬编码,具体取决于所选的选项值。我试图做这样的事情:

$("#Units").on("change", function () {
  if ($(this).val() !== "Finance") {
    $(".dissable").val("3").prop("disabled", true); 
    $(".dissable").val("4").prop("disabled", true); 
  } else {
    $(".dissable").val("3").prop("disabled", false);
    $(".dissable").val("4").prop("disabled", false);
  }
}).trigger('change');
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>

<select id="Units">
  <option value="Marketing" > Marketing </option>
  <option value="Finance" > Finance </option>
  <option value="Operations" > Operations </option>
</select>

<input type="checkbox" class="dissable" onclick="check();" value="1"> chbx1
<input type="checkbox" class="dissable" onclick="check();" value="2"> chbx2
<input type="checkbox" class="dissable" onclick="check();" value="3"> chbx3
<input type="checkbox" class="dissable" onclick="check();" value="4"> chbx4

But no desired effect, which is supposed to be like this:

但没有预期的效果,应该是这样的:

  • When I choose an option other then "Finance", checkbox no. 3 and 4 are disabled.
  • 当我选择其他选项时,“财务”,复选框号。 3和4被禁用。

  • For "Finance", all checkboxes are enabled.
  • 对于“财务”,将启用所有复选框。

I need to do this based on the checkbox values, not only class.

我需要根据复选框值执行此操作,而不仅仅是类。

3 个解决方案

#1


Use the following selector:

使用以下选择器:

$(".dissable[value='3']").prop("disabled", true);

The [value='3'] creates a selection based on the value. See working example:

[value ='3']根据值创建选择。见工作示例:

$("#Units").on("change", function () {
  if ($(this).val() !== "Finance") {
    $(".dissable[value='3']").prop("disabled", true); 
    $(".dissable[value='4']").prop("disabled", true); 
  } else {
    $(".dissable[value='3']").prop("disabled", false);
    $(".dissable[value='4']").prop("disabled", false);
  }
}).trigger('change');
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>

<select id="Units">
  <option value="Marketing" > Marketing </option>
  <option value="Finance" > Finance </option>
  <option value="Operations" > Operations </option>
</select>

<input type="checkbox" class="dissable" onclick="check();" value="1" /> chbx1
<input type="checkbox" class="dissable" onclick="check();" value="2" /> chbx2
<input type="checkbox" class="dissable" onclick="check();" value="3" /> chbx3
<input type="checkbox" class="dissable" onclick="check();" value="4" /> chbx4
(fiddle: http://jsfiddle.net/2sw3q8p4/)

#2


You need to filter check-boxes based on values so use $.fn.filter. Currently you are setting its value using $(".dissable").val("3")

您需要根据值过滤复选框,因此请使用$ .fn.filter。目前,您使用$(“。dissable”)设置其值.val(“3”)

Reduce the set of matched elements to those that match the selector or pass the function's test.

将匹配元素集减少到与选择器匹配的元素或传递函数的测试。

Code

$("#Units").on("change", function () {
    //Filter checkboxes whose value is 3 or 4
    $(".dissable").filter(function(){ 
        return $(this).val() == 3 || $(this).val() == 4;
    }).prop("disabled", $(this).val() == "Finance");  //disable or enable based on condition
}).trigger('change');

DEMO

#3


Try caching $(".dissable") , utilizing .slice() on cached selector

尝试缓存$(“。dissable”),在缓存选择器上使用.slice()

function check() {}

var disable = $(".dissable");

$("#Units").on("change", function() {
  if ($(this).val() !== "Finance") {
    // disable `.dissable` at index `2,` `3`
    disable.slice(2, 4).prop("disabled", true);
  } else {
    // enable `.dissable` at index `2,` `3`
    disable.slice(2, 4).prop("disabled", false);
  }
}).trigger("change");
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js">
</script>
<select id="Units">
  <option value="Marketing">Marketing</option>
  <option value="Finance">Finance</option>
  <option value="Operations">Operations</option>
</select>

<input type="checkbox" class="dissable" onclick="check();" value="1">chbx1
<input type="checkbox" class="dissable" onclick="check();" value="2">chbx2
<input type="checkbox" class="dissable" onclick="check();" value="3">chbx3
<input type="checkbox" class="dissable" onclick="check();" value="4">chbx4

#1


Use the following selector:

使用以下选择器:

$(".dissable[value='3']").prop("disabled", true);

The [value='3'] creates a selection based on the value. See working example:

[value ='3']根据值创建选择。见工作示例:

$("#Units").on("change", function () {
  if ($(this).val() !== "Finance") {
    $(".dissable[value='3']").prop("disabled", true); 
    $(".dissable[value='4']").prop("disabled", true); 
  } else {
    $(".dissable[value='3']").prop("disabled", false);
    $(".dissable[value='4']").prop("disabled", false);
  }
}).trigger('change');
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>

<select id="Units">
  <option value="Marketing" > Marketing </option>
  <option value="Finance" > Finance </option>
  <option value="Operations" > Operations </option>
</select>

<input type="checkbox" class="dissable" onclick="check();" value="1" /> chbx1
<input type="checkbox" class="dissable" onclick="check();" value="2" /> chbx2
<input type="checkbox" class="dissable" onclick="check();" value="3" /> chbx3
<input type="checkbox" class="dissable" onclick="check();" value="4" /> chbx4
(fiddle: http://jsfiddle.net/2sw3q8p4/)

#2


You need to filter check-boxes based on values so use $.fn.filter. Currently you are setting its value using $(".dissable").val("3")

您需要根据值过滤复选框,因此请使用$ .fn.filter。目前,您使用$(“。dissable”)设置其值.val(“3”)

Reduce the set of matched elements to those that match the selector or pass the function's test.

将匹配元素集减少到与选择器匹配的元素或传递函数的测试。

Code

$("#Units").on("change", function () {
    //Filter checkboxes whose value is 3 or 4
    $(".dissable").filter(function(){ 
        return $(this).val() == 3 || $(this).val() == 4;
    }).prop("disabled", $(this).val() == "Finance");  //disable or enable based on condition
}).trigger('change');

DEMO

#3


Try caching $(".dissable") , utilizing .slice() on cached selector

尝试缓存$(“。dissable”),在缓存选择器上使用.slice()

function check() {}

var disable = $(".dissable");

$("#Units").on("change", function() {
  if ($(this).val() !== "Finance") {
    // disable `.dissable` at index `2,` `3`
    disable.slice(2, 4).prop("disabled", true);
  } else {
    // enable `.dissable` at index `2,` `3`
    disable.slice(2, 4).prop("disabled", false);
  }
}).trigger("change");
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js">
</script>
<select id="Units">
  <option value="Marketing">Marketing</option>
  <option value="Finance">Finance</option>
  <option value="Operations">Operations</option>
</select>

<input type="checkbox" class="dissable" onclick="check();" value="1">chbx1
<input type="checkbox" class="dissable" onclick="check();" value="2">chbx2
<input type="checkbox" class="dissable" onclick="check();" value="3">chbx3
<input type="checkbox" class="dissable" onclick="check();" value="4">chbx4