英文:
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 -->
<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 />
<!-- 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 = $('[id^=Chk]:not(#Chk0)');
if ($('#Chk0').is(':checked'))
otherCheckboxes.prop('checked', true);
else
otherCheckboxes.prop('checked', 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 -->
$("#Chk0").click(function () {
$('[id^=Chk]:not(#Chk0)').prop('checked', $(this).prop('checked')).prop('disabled', $(this).prop('checked'));
});
<!-- language: lang-html -->
<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 />
<!-- 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:
<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/>
and your function should be:
function ValueAnyDisable()
{
if ($('#Chk0').is(':checked')) {
$('input').prop('checked', true);
} else {
$('input').prop('checked', false);
}
}
You can also do it with ID selector.
You can remove onclik ="ValueAnyDisable()" :
<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/>
and your JQuery event change to :
$(document).ready(function(){
$('#Chk0').click(function() {
if ($(this).is(':checked')) {
$('input').prop('checked', true);
} else {
$('input').prop('checked', 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 <label>
tag. And created a minimal function for checkboxes change event:
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
$(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);
}
}
});
<!-- language: lang-html -->
<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>
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论