Checking Any checkbox is causing all the individual checkboxes to be unresponsive.

huangapple go评论55阅读模式
英文:

Checking Any checkbox is causing all the individual checkboxes to be unresponsive

问题

以下是您提供的内容的翻译:

我在我的网页上有以下复选框:

<input type="checkbox" onclick="ValueAnyDisable()" id="Chk0" name="Test1" value="1"  />
Any  <br />
<input type="checkbox" onclick="ValueAnyDisable()" id="Chk1" name="Test1" value="12"  />
BMW <br />
<input type="checkbox" onclick="ValueAnyDisable()" id="Chk2" name="Test1" value="11"  />
FIAT <br />
<input type="checkbox" onclick="ValueAnyDisable()" id="Chk3" name="Test1" value="13"  />
Tesla  <br />
<input type="checkbox" onclick="ValueAnyDisable()" id="Chk4" name="Test1" value="54"  />
Toyota <br />
<input type="checkbox" onclick="ValueAnyDisable()" id="Chk5" name="Test1" value="44"  />
Ford <br />

如果有人勾选了"Any"复选框,我希望所有其他复选框也被选中。如果有人取消勾选"Any"复选框,我希望其他复选框也被取消选中。以下是我尝试编写的代码以实现此目标:

function ValueAnyDisable()
{
    const otherCheckboxes = $('[id^=Chk]:not(#Chk0)');
    if ($('#Chk0').is(':checked'))
        otherCheckboxes.prop('checked', true);
    else
        otherCheckboxes.prop('checked', false);

}

上述代码在勾选"Any"时选中所有复选框,在取消勾选"Any"时取消选中所有复选框。但是,在取消勾选"Any"复选框后,我无法单独选中任何复选框。所有其他复选框都变得无响应。我在循环中创建这些复选框,值来自数据库,因此我只能为所有复选框放置一个onclick事件。以上是生成的HTML代码。

英文:

I have the following checkboxes on my web page:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-html -->

&lt;input type=&quot;checkbox&quot; onclick=&quot;ValueAnyDisable()&quot; id=&quot;Chk0&quot; name=&quot;Test1&quot; value=&quot;1&quot;  /&gt;
Any  &lt;br /&gt;
&lt;input type=&quot;checkbox&quot; onclick=&quot;ValueAnyDisable()&quot; id=&quot;Chk1&quot; name=&quot;Test1&quot; value=&quot;12&quot;  /&gt;
BMW &lt;br /&gt;
&lt;input type=&quot;checkbox&quot; onclick=&quot;ValueAnyDisable()&quot; id=&quot;Chk2&quot; name=&quot;Test1&quot; value=&quot;11&quot;  /&gt;
FIAT &lt;br /&gt;
&lt;input type=&quot;checkbox&quot; onclick=&quot;ValueAnyDisable()&quot; id=&quot;Chk3&quot; name=&quot;Test1&quot; value=&quot;13&quot;  /&gt;
Tesla  &lt;br /&gt;
&lt;input type=&quot;checkbox&quot; onclick=&quot;ValueAnyDisable()&quot; id=&quot;Chk4&quot; name=&quot;Test1&quot; value=&quot;54&quot;  /&gt;
Toyota &lt;br /&gt;
&lt;input type=&quot;checkbox&quot; onclick=&quot;ValueAnyDisable()&quot; id=&quot;Chk5&quot; name=&quot;Test1&quot; value=&quot;44&quot;  /&gt;
Ford &lt;br /&gt;

<!-- end snippet -->

If someone checks the "Any" check box then I want all the other checkboxes to be checked too. If someone unchecks the "Any" checkbox then I want other checkboxes to be unselected.
This is what I tried to write in order to achieve this:

 function ValueAnyDisable()
        {
            const otherCheckboxes = $(&#39;[id^=Chk]:not(#Chk0)&#39;);
            if ($(&#39;#Chk0&#39;).is(&#39;:checked&#39;))
                otherCheckboxes.prop(&#39;checked&#39;, true);
            else
                otherCheckboxes.prop(&#39;checked&#39;, false);

        }

the above code checks all the checkboxes when "Any" is checked and uncheck all the checkboxes when "Any" is unchecked, but after I uncheck the "Any" checkbox, I cannot individually check any check box. All other checkboxes become unresponsive. I am creating these checkboxes in a loop and the value is coming from the database so I can only put one onclick event for all the checkboxes. The above check boxes is the rendered HTML.

答案1

得分: 1

在您的ValueAnyDisable函数中,您根据Chk0是否被选中来设置所有复选框的选中或取消选中状态。其他复选框没有独立的"生命"。通过在所有其他复选框的onclick事件中调用相同的函数,您实际上将所有其他复选框限制为Chk0的值。

由于您只需要监听Chk0的选中状态以实现这个特定的功能,您可以仅为Chk0运行一个点击函数,并根据Chk0的值设置其他复选框的值为true或false。由于这个函数只会在Chk0上调用,其他复选框不会受到其个别的选中操作的限制。

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input type="checkbox" id="Chk0" name="Test1" value="1" />
Any  <br />
<input type="checkbox" id="Chk1" name="Test1" value="12" />
BMW <br />
<input type="checkbox" id="Chk2" name="Test1" value="11" />
FIAT <br />
<input type="checkbox" id="Chk3" name="Test1" value="13" />
Tesla  <br />
<input type="checkbox" id="Chk4" name="Test1" value="54" />
Toyota <br />
<input type="checkbox" id="Chk5" name="Test1" value="44" />
Ford <br />
$("#Chk0").click(function () {
    $('[id^=Chk]:not(#Chk0)').prop('checked', $(this).prop('checked')).prop('disabled', $(this).prop('checked'));
});

这段代码将根据Chk0的选中状态设置其他复选框的选中状态,并禁用或启用它们。

英文:

In your function ValueAnyDisable, you are setting check or uncheck of all the checkboxes depending on whether Chk0 is checked or not. There is no individual "life" for other checkboxes. By calling this same function onclick of all other checkboxes, in a way, you have restricted all other checkboxes to the value of Chk0.

Since you just need to listen for check of Chk0 for this particular functionality, you can run a click function just for on Chk0, and set values of other checkboxes true or false depending on the value of Chk0. And since this function will be only called on Chk0, other checkboxes will not be restricted of their individual check actions.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

$(&quot;#Chk0&quot;).click(function () {
        $(&#39;[id^=Chk]:not(#Chk0)&#39;).prop(&#39;checked&#39;, $(this).prop(&#39;checked&#39;)).prop(&#39;disabled&#39;, $(this).prop(&#39;checked&#39;));
    });

<!-- language: lang-html -->

&lt;script src=&quot;https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js&quot;&gt;&lt;/script&gt;
&lt;input type=&quot;checkbox&quot; id=&quot;Chk0&quot; name=&quot;Test1&quot; value=&quot;1&quot;  /&gt;
Any  &lt;br /&gt;
&lt;input type=&quot;checkbox&quot; id=&quot;Chk1&quot; name=&quot;Test1&quot; value=&quot;12&quot;  /&gt;
BMW &lt;br /&gt;
&lt;input type=&quot;checkbox&quot; id=&quot;Chk2&quot; name=&quot;Test1&quot; value=&quot;11&quot;  /&gt;
FIAT &lt;br /&gt;
&lt;input type=&quot;checkbox&quot; id=&quot;Chk3&quot; name=&quot;Test1&quot; value=&quot;13&quot;  /&gt;
Tesla  &lt;br /&gt;
&lt;input type=&quot;checkbox&quot; id=&quot;Chk4&quot; name=&quot;Test1&quot; value=&quot;54&quot;  /&gt;
Toyota &lt;br /&gt;
&lt;input type=&quot;checkbox&quot; id=&quot;Chk5&quot; name=&quot;Test1&quot; value=&quot;44&quot;  /&gt;
Ford &lt;br /&gt;

<!-- end snippet -->

答案2

得分: 1

你需要从另一个输入复选框中移除onclick事件,这样你的HTML将如下所示:

<input type="checkbox" onclick="ValueAnyDisable()" id="Chk0" name="Test1" value="1" />Any  <br/>
<input type="checkbox" id="Chk1" name="Test1" value="12" />BMW <br/>
<input type="checkbox" id="Chk2" name="Test1" value="11" />FIAT <br/>
<input type="checkbox" id="Chk3" name="Test1" value="13" />Tesla <br/>
<input type="checkbox" id="Chk4" name="Test1" value="54" />Toyota <br/>
<input type="checkbox" id="Chk5" name="Test1" value="44" />Ford <br/>

你的函数应该是:

function ValueAnyDisable() {   
    if ($('#Chk0').is(':checked')) {
        $('input').prop('checked', true);
    } else {
        $('input').prop('checked', false);
    }
}

你也可以使用ID选择器来完成它。你可以移除onclik="ValueAnyDisable()",然后你的HTML如下:

<input type="checkbox" id="Chk0" name="Test1" value="1" />Any  <br/>
<input type="checkbox" id="Chk1" name="Test1" value="12" />BMW <br/>
<input type="checkbox" id="Chk2" name="Test1" value="11" />FIAT <br/>
<input type="checkbox" id="Chk3" name="Test1" value="13" />Tesla <br/>
<input type="checkbox" id="Chk4" name="Test1" value="54" />Toyota <br/>
<input type="checkbox" id="Chk5" name="Test1" value="44" />Ford <br/>

然后你的jQuery事件改为:

$(document).ready(function(){
    $('#Chk0').click(function() {
        if ($(this).is(':checked')) {
            $('input').prop('checked', true);
        } else {
            $('input').prop('checked', false);
        }
    });  
});
英文:

First you have to remove the onclick event from another input checkbox so your HTML will be look like:

&lt;input type=&quot;checkbox&quot; onclick=&quot;ValueAnyDisable()&quot; id=&quot;Chk0&quot; name=&quot;Test1&quot; value=&quot;1&quot; /&gt;Any  &lt;br/&gt;
&lt;input type=&quot;checkbox&quot; id=&quot;Chk1&quot; name=&quot;Test1&quot; value=&quot;12&quot;  /&gt;BMW &lt;br/&gt;
&lt;input type=&quot;checkbox&quot; id=&quot;Chk2&quot; name=&quot;Test1&quot; value=&quot;11&quot;  /&gt;FIAT &lt;br/&gt;
&lt;input type=&quot;checkbox&quot; id=&quot;Chk3&quot; name=&quot;Test1&quot; value=&quot;13&quot;  /&gt;Tesla &lt;br/&gt;
&lt;input type=&quot;checkbox&quot; id=&quot;Chk4&quot; name=&quot;Test1&quot; value=&quot;54&quot;  /&gt;Toyota &lt;br/&gt;
&lt;input type=&quot;checkbox&quot; id=&quot;Chk5&quot; name=&quot;Test1&quot; value=&quot;44&quot;  /&gt;Ford &lt;br/&gt;

and your function should be:

function ValueAnyDisable()
    {   
        if ($(&#39;#Chk0&#39;).is(&#39;:checked&#39;)) {
                $(&#39;input&#39;).prop(&#39;checked&#39;, true);
            } else {
                $(&#39;input&#39;).prop(&#39;checked&#39;, false);
            }
    }

You can also do it with ID selector.
You can remove onclik ="ValueAnyDisable()" :

&lt;input type=&quot;checkbox&quot; id=&quot;Chk0&quot; name=&quot;Test1&quot; value=&quot;1&quot; /&gt;Any  &lt;br/&gt;
&lt;input type=&quot;checkbox&quot; id=&quot;Chk1&quot; name=&quot;Test1&quot; value=&quot;12&quot;  /&gt;BMW &lt;br/&gt;
&lt;input type=&quot;checkbox&quot; id=&quot;Chk2&quot; name=&quot;Test1&quot; value=&quot;11&quot;  /&gt;FIAT &lt;br/&gt;
&lt;input type=&quot;checkbox&quot; id=&quot;Chk3&quot; name=&quot;Test1&quot; value=&quot;13&quot;  /&gt;Tesla &lt;br/&gt;
&lt;input type=&quot;checkbox&quot; id=&quot;Chk4&quot; name=&quot;Test1&quot; value=&quot;54&quot;  /&gt;Toyota &lt;br/&gt;
&lt;input type=&quot;checkbox&quot; id=&quot;Chk5&quot; name=&quot;Test1&quot; value=&quot;44&quot;  /&gt;Ford &lt;br/&gt;

and your JQuery event change to :

$(document).ready(function(){
	$(&#39;#Chk0&#39;).click(function() {
		if ($(this).is(&#39;:checked&#39;)) {
			$(&#39;input&#39;).prop(&#39;checked&#39;, true);
		} else {
			$(&#39;input&#39;).prop(&#39;checked&#39;, false);
		}
	});  
});

答案3

得分: 1

我改进了你的HTML,添加了一个<label>标签,并为复选框的更改事件创建了一个简单的函数:

$(document).on('change', '[name="Test1"]', function() {
  const anyCheckbox = $('#Chk0');
  const checkboxes = $('[name="Test1"]').not(anyCheckbox);
  if ($(this).is(anyCheckbox)) {
    if ($(this).prop('checked')) {
      checkboxes.prop('checked', true);
    } else {
      checkboxes.prop('checked', false);
    }
  } else {
    if ($('[name="Test1"]:checked').not(anyCheckbox).length === checkboxes.length) {
      anyCheckbox.prop('checked', true);
    } else {
      anyCheckbox.prop('checked', false);
    }
  }
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input type="checkbox" id="Chk0" name="Test1" value="1">
<label for="Chk0">Any</label>
<br>
<input type="checkbox" id="Chk1" name="Test1" value="12">
<label for="Chk1">BMW</label>
<br>
<input type="checkbox" id="Chk2" name="Test1" value="11">
<label for="">FIAT</label>
<br>
<input type="checkbox" id="Chk3" name="Test1" value="13">
<label for="Chk2">Tesla</label>
<br>
<input type="checkbox" id="Chk4" name="Test1" value="54">
<label for="Chk4">Toyota</label>
<br>
<input type="checkbox" id="Chk5" name="Test1" value="44">
<label for="Chk5">Ford</label>

希望这对你有所帮助。

英文:

Improved your html a bit by adding a &lt;label&gt; tag. And created a minimal function for checkboxes change event:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

$(document).on(&#39;change&#39;, &#39;[name=&quot;Test1&quot;]&#39;, function() {
  const anyCheckbox = $(&#39;#Chk0&#39;);
  const checkboxes = $(&#39;[name=&quot;Test1&quot;]&#39;).not(anyCheckbox);
  if ( $(this).is(anyCheckbox) ) {
    if ( $(this).prop(&#39;checked&#39;) ) {
      checkboxes.prop(&#39;checked&#39;, true);
    } else {
      checkboxes.prop(&#39;checked&#39;, false);
    }
  } else {
    if ( $(&#39;[name=&quot;Test1&quot;]:checked&#39;).not(anyCheckbox).length === checkboxes.length ) {
      anyCheckbox.prop(&#39;checked&#39;, true);
    } else {
      anyCheckbox.prop(&#39;checked&#39;, false);
    }
  }
});

<!-- language: lang-html -->

&lt;script src=&quot;https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js&quot;&gt;&lt;/script&gt;
&lt;input type=&quot;checkbox&quot; id=&quot;Chk0&quot; name=&quot;Test1&quot; value=&quot;1&quot;&gt;
&lt;label for=&quot;Chk0&quot;&gt;Any&lt;/label&gt;
&lt;br&gt;
&lt;input type=&quot;checkbox&quot; id=&quot;Chk1&quot; name=&quot;Test1&quot; value=&quot;12&quot;&gt;
&lt;label for=&quot;Chk1&quot;&gt;BMW&lt;/label&gt;
&lt;br&gt;
&lt;input type=&quot;checkbox&quot; id=&quot;Chk2&quot; name=&quot;Test1&quot; value=&quot;11&quot;&gt;
&lt;label for=&quot;&quot;&gt;FIAT&lt;/label&gt;
&lt;br&gt;
&lt;input type=&quot;checkbox&quot; id=&quot;Chk3&quot; name=&quot;Test1&quot; value=&quot;13&quot;&gt;
&lt;label for=&quot;Chk2&quot;&gt;Tesla&lt;/label&gt;
&lt;br&gt;
&lt;input type=&quot;checkbox&quot; id=&quot;Chk4&quot; name=&quot;Test1&quot; value=&quot;54&quot;&gt;
&lt;label for=&quot;Chk4&quot;&gt;Toyota&lt;/label&gt;
&lt;br&gt;
&lt;input type=&quot;checkbox&quot; id=&quot;Chk5&quot; name=&quot;Test1&quot; value=&quot;44&quot;&gt;
&lt;label for=&quot;Chk5&quot;&gt;Ford&lt;/label&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年5月13日 12:24:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/76241069.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定