I want to check whether checkbox is checked or not using jquery. I want to check it on checkbox's onclick event.
我想检查是否使用jquery选中复选框。我想在复选框的onclick事件上查看它。
<input type="checkbox" onclick="javascript:check_action();" id="Public(Web)" checked="checked" value="anyone" name="data[anyone]">
Is it possible? How?
可能吗?怎么样?
Thanks.
谢谢。
9 个解决方案
#1
9
First, don't use javascript:
in event handler attributes. It's wrong and only works because it happens to be valid JavaScript syntax. Second, your id
is not valid. Parentheses are not allowed in the id
attribute (in HTML 4 at least; HTML 5 lifts this restriction). Third, if you're using jQuery it probably makes sense to use its click()
method to handle the click
event, although be aware that changing it to do that will mean that if the user clicks on the checkbox before the document has loaded then your script won't handle it.
首先,不要在事件处理程序属性中使用javascript:它是错误的,只有它才有效,因为它恰好是有效的JavaScript语法。其次,你的身份证无效。 id属性中不允许使用括号(至少在HTML 4中; HTML 5解除了此限制)。第三,如果你正在使用jQuery,使用它的click()方法来处理click事件可能是有意义的,虽然要知道改变它来做那件事意味着如果用户在文档加载之前单击复选框那么你的脚本不会处理它。
<input type="checkbox" id="Public_Web" checked value="anyone"
name="data[anyone]">
$(document).ready(function() {
$("#Public_Web").click(function() {
if (this.checked) {
alert("Checked!");
}
});
});
#2
8
You can also use this
你也可以用这个
if($("#checkbox_id").is(':checked'))
#3
4
You can do like this
你可以这样做
$('#checkbox_id').click(function(){
alert(this.checked);
});
Or using is()
method:
或者使用is()方法:
$('#checkbox_id').click(function(){
if ($(this).is(':checked')){
alert('Checked');
}
else{
alert('Not Checked');
}
});
If you want to do this for all checkbox inside a form, you can use the :checkbox
filter selector like this:
如果要对表单内的所有复选框执行此操作,可以使用:checkbox过滤器选择器,如下所示:
$('#form_id :checkbox').click(function(){
alert(this.checked);
});
Make sure to wrap your code in ready handler:
确保将代码包装在ready处理程序中:
$(function(){
// code....
});
#4
2
There are Several options are there like....
有几个选项有...
1. if($("#ans").is('checked'))
2. if($("#ans:checked"))
3. if($('input:checkbox:checked'))
#5
1
Firstly, bind the event in script tags, rather than inline. This is much easier to follow and makes your HTML far more readable. Then you can use the jQuery selector Then you can use the :checked
to determine whether the checkbox is checked.checked
attribute of the element.
首先,将事件绑定在脚本标记中,而不是内联。这更容易理解,使您的HTML更具可读性。然后你可以使用jQuery选择器:选中以确定是否选中了复选框。然后,您可以使用元素的checked属性。
$(document).ready(function(){
$('#Public(Web)').click(function(){
if (this.checked) {
// do your code here
}
});
});
#6
1
Try This:
尝试这个:
if(!$('#elementid').is(':checked')){
alert('Not checked');
}else{
alert('checked');
}
#7
1
You can do this using is()
method:
您可以使用is()方法执行此操作:
$('##Public_Web').click(function(){
if ($(this).is(':checked')){
alert('Checked');
}
else{
alert('Not Checked');
}
});
If you want to do this for all checkbox inside a form, you can use the :checkbox
filter selector:
如果要对表单内的所有复选框执行此操作,可以使用:checkbox过滤器选择器:
$('#form_id :checkbox').click(function(){
alert(this.checked);
});
Make sure to wrap your code in document ready handler:
确保将代码包装在文档就绪处理程序中:
$(document).ready(function() {
// code....
});
#8
1
html
HTML
<input type="checkbox" id="chkBox" checked/> Change Check Box
jquery
jQuery的
$("#chkBox").change(function() {
if (this.checked) {
alert("Checked");
}
else {
alert("Not Checked")
}
});
$("#chkBox").change(function() {
if (this.checked) {
alert("Checked");
}
else {
alert("Not Checked")
}
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="checkbox" id="chkBox" checked/> Change Check Box
#9
0
Here is another example, notice instead of using a onClick event in the Element itself i would prefer to use a listener, but remove the brackets from the id, It will give you a hard time in some browsers.
这是另一个例子,通知而不是在Element本身中使用onClick事件我宁愿使用一个监听器,但是从id中删除括号,这会让你在某些浏览器中遇到困难。
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html><head>
<input type="checkbox" id="PublicWeb" checked="checked" value="anyone" name="data[anyone]" />
<script type='text/javascript' src='http://code.jquery.com/jquery-latest.min.js'></script>
<script>
$(document).ready(function(){
$('#PublicWeb').click(function(){
if($(this).is(':checked')){
alert("its checked alright");
}
});
});
</script>
</head><body></body></html>
#1
9
First, don't use javascript:
in event handler attributes. It's wrong and only works because it happens to be valid JavaScript syntax. Second, your id
is not valid. Parentheses are not allowed in the id
attribute (in HTML 4 at least; HTML 5 lifts this restriction). Third, if you're using jQuery it probably makes sense to use its click()
method to handle the click
event, although be aware that changing it to do that will mean that if the user clicks on the checkbox before the document has loaded then your script won't handle it.
首先,不要在事件处理程序属性中使用javascript:它是错误的,只有它才有效,因为它恰好是有效的JavaScript语法。其次,你的身份证无效。 id属性中不允许使用括号(至少在HTML 4中; HTML 5解除了此限制)。第三,如果你正在使用jQuery,使用它的click()方法来处理click事件可能是有意义的,虽然要知道改变它来做那件事意味着如果用户在文档加载之前单击复选框那么你的脚本不会处理它。
<input type="checkbox" id="Public_Web" checked value="anyone"
name="data[anyone]">
$(document).ready(function() {
$("#Public_Web").click(function() {
if (this.checked) {
alert("Checked!");
}
});
});
#2
8
You can also use this
你也可以用这个
if($("#checkbox_id").is(':checked'))
#3
4
You can do like this
你可以这样做
$('#checkbox_id').click(function(){
alert(this.checked);
});
Or using is()
method:
或者使用is()方法:
$('#checkbox_id').click(function(){
if ($(this).is(':checked')){
alert('Checked');
}
else{
alert('Not Checked');
}
});
If you want to do this for all checkbox inside a form, you can use the :checkbox
filter selector like this:
如果要对表单内的所有复选框执行此操作,可以使用:checkbox过滤器选择器,如下所示:
$('#form_id :checkbox').click(function(){
alert(this.checked);
});
Make sure to wrap your code in ready handler:
确保将代码包装在ready处理程序中:
$(function(){
// code....
});
#4
2
There are Several options are there like....
有几个选项有...
1. if($("#ans").is('checked'))
2. if($("#ans:checked"))
3. if($('input:checkbox:checked'))
#5
1
Firstly, bind the event in script tags, rather than inline. This is much easier to follow and makes your HTML far more readable. Then you can use the jQuery selector Then you can use the :checked
to determine whether the checkbox is checked.checked
attribute of the element.
首先,将事件绑定在脚本标记中,而不是内联。这更容易理解,使您的HTML更具可读性。然后你可以使用jQuery选择器:选中以确定是否选中了复选框。然后,您可以使用元素的checked属性。
$(document).ready(function(){
$('#Public(Web)').click(function(){
if (this.checked) {
// do your code here
}
});
});
#6
1
Try This:
尝试这个:
if(!$('#elementid').is(':checked')){
alert('Not checked');
}else{
alert('checked');
}
#7
1
You can do this using is()
method:
您可以使用is()方法执行此操作:
$('##Public_Web').click(function(){
if ($(this).is(':checked')){
alert('Checked');
}
else{
alert('Not Checked');
}
});
If you want to do this for all checkbox inside a form, you can use the :checkbox
filter selector:
如果要对表单内的所有复选框执行此操作,可以使用:checkbox过滤器选择器:
$('#form_id :checkbox').click(function(){
alert(this.checked);
});
Make sure to wrap your code in document ready handler:
确保将代码包装在文档就绪处理程序中:
$(document).ready(function() {
// code....
});
#8
1
html
HTML
<input type="checkbox" id="chkBox" checked/> Change Check Box
jquery
jQuery的
$("#chkBox").change(function() {
if (this.checked) {
alert("Checked");
}
else {
alert("Not Checked")
}
});
$("#chkBox").change(function() {
if (this.checked) {
alert("Checked");
}
else {
alert("Not Checked")
}
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="checkbox" id="chkBox" checked/> Change Check Box
#9
0
Here is another example, notice instead of using a onClick event in the Element itself i would prefer to use a listener, but remove the brackets from the id, It will give you a hard time in some browsers.
这是另一个例子,通知而不是在Element本身中使用onClick事件我宁愿使用一个监听器,但是从id中删除括号,这会让你在某些浏览器中遇到困难。
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html><head>
<input type="checkbox" id="PublicWeb" checked="checked" value="anyone" name="data[anyone]" />
<script type='text/javascript' src='http://code.jquery.com/jquery-latest.min.js'></script>
<script>
$(document).ready(function(){
$('#PublicWeb').click(function(){
if($(this).is(':checked')){
alert("its checked alright");
}
});
});
</script>
</head><body></body></html>