jquery 检查单选按钮是否被选中,然后检查复选框是否被选中
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23655814/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
jquery check if radio button is checked and then check if tickbox is checked
提问by 001221
Hi, I have a two part form which I have developed. One is for existing members and one is not.
嗨,我有一个由两部分组成的表格,我已经开发了。一种是针对现有成员的,一种不是。
On one of the forms I have a checkbox which will need to be checked before they can proceed, to determine that they are on the correct form. I will use the radio button which hides and unhides forms.
在其中一个表格上,我有一个复选框,需要在他们继续之前进行检查,以确定它们是否在正确的表格上。我将使用隐藏和取消隐藏表单的单选按钮。
However when I try to get a handle on if the radio button is checked and then checking to see if the checkbox is checked I get nothing returned.
但是,当我尝试处理单选按钮是否被选中然后检查复选框是否被选中时,我没有得到任何返回。
Can someone help me implement this? My JS is below or view my JSFIDDLE:
有人可以帮我实现这个吗?我的 JS 在下面或查看我的JSFIDDLE:
$(document).ready(function(){
var check;
if($("input:radio[name=existingpartner]").is(":checked")){
// Example 3 - With jQuery prop
$("#test-with-prop").on("click", function(){
check = $("#mycheckbox").prop("checked");
if(check) {
alert("Checkbox is checked.");
} else {
alert("Checkbox is unchecked.");
}
});
});
回答by Anton
In your if statement you are checking if the element is checked, but in the DOM the element is not checked therefore it will not bind the event on the button.
在您的 if 语句中,您正在检查元素是否被选中,但在 DOM 中元素未被选中,因此它不会绑定按钮上的事件。
Move the if statement inside the bind event.
将 if 语句移动到绑定事件中。
$("#test-with-prop").on("click", function () {
if ($("input:radio[name=existingpartner]").is(":checked")) {
check = $("#mycheckbox").prop("checked");
if (check) {
alert("Checkbox is checked.");
} else {
alert("Checkbox is unchecked.");
}
}
});