提问人:Clinton Green 提问时间:11/1/2011 最后编辑:Mateen UlhaqClinton Green 更新时间:10/17/2023 访问量:1362051
jQuery(如果选中复选框)
jQuery if checkbox is checked
问:
我在下面有一个函数,我只想在选中相同的复选框时触发。tr
$(".add_menu_item_table").live('click', function() {
if ($('input.checkbox_check').attr(':checked')); {
// I want this to trigger.
}
});
<table id="table-data">
<tbody>
<tr>
<td><input type="checkbox" class="checkbox_check"></td>
<td><input type="button" class="add_menu_item_table" value="Add"></td>
</tr>
<tr>
<td><input type="checkbox" class="checkbox_check"></td>
<td><input type="button" class="add_menu_item_table" value="Add"></td>
</tr>
</tbody>
</table>
答:
if ($('input.checkbox_check').is(':checked')) {
评论
if ( elem.checked )
if ( $( elem ).prop( "checked" ) )
if ( $( elem ).is( ":checked" ) )
if ($('#my_input:checked))
对于 jQuery 1.6 或更高版本:
if ($('input.checkbox_check').prop('checked')) {
//blah blah
}
用于确定是否选中复选框的跨浏览器兼容方法 是使用属性 https://api.jquery.com/prop/
评论
验证是否选中了它
$('#checkboxId').is(':checked')
要检查
$("#checkboxId").prop('checked', true)
取消选中
$("#checkboxId").prop('checked', false)
查看 ATTR 之间的主要区别 |道具 |如下:
来源:http://api.jquery.com/attr/
$( "input" )
.change(function() {
var $input = $( this );
$( "p" ).html( ".attr( 'checked' ): <b>" + $input.attr( "checked" ) + "</b><br>" +
".prop( 'checked' ): <b>" + $input.prop( "checked" ) + "</b><br>" +
".is( ':checked' ): <b>" + $input.is( ":checked" ) + "</b>" );
})
.change();
p {
margin: 20px 0 0;
}
b {
color: blue;
}
<meta charset="utf-8">
<title>attr demo</title>
<script src="https://code.jquery.com/jquery-1.10.2.js"></script>
</head>
<body>
<input id="check1" type="checkbox" checked="checked">
<label for="check1">Check me</label>
<p></p>
</body>
</html>
如果上述解决方案都因任何原因而不起作用,就像我的情况一样,请尝试以下方法:
<script type="text/javascript">
$(function()
{
$('[name="my_checkbox"]').change(function()
{
if ($(this).is(':checked')) {
// Do something...
alert('You can rock now...');
};
});
});
</script>
如果选中:
$( "SELECTOR" ).attr( "checked" ) // Returns ‘true’ if present on the element, returns undefined if not present
$( "SELECTOR" ).prop( "checked" ) // Returns true if checked, false if unchecked.
$( "SELECTOR" ).is( ":checked" ) // Returns true if checked, false if unchecked.
获取检查值:
$( "SELECTOR:checked" ).val()
获取选中的 val 编号:
$( "SELECTOR:checked" ).length
选中或取消选中复选框
$( "SELECTOR" ).prop( "disabled", false );
$( "SELECTOR" ).prop( "checked", true );
评论
$( "SELECTOR:checked" ).val()
如果选中,则为您提供“打开”,如果未选中,则为您提供“” - 有用!value="on"
要检查输入并通过复选框进行确认,请使用此脚本...
$(document).on("change", ".inputClass", function () {
if($(this).is(':checked')){
confirm_message = $(this).data('confirm');
var confirm_status = confirm(confirm_message);
if (confirm_status == true) {
//doing somethings...
}
}});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<label> check action </lable>
<input class="inputClass" type="checkbox" data-confirm="are u sure to do ...?" >
New to jQuery/Javascript and tried the above solutions. Eventually, this is what worked for me to:
- keep elements hidden upon loading page (because checkboxes = unchecked by default), and then
- have elements show/hide based on whether the checkbox is checked or not.
It's probably a little ugly/redundant/extraneous syntax-wise, but it works for now!
Note: this all goes in the following tag, right before the tag on your HTML page:<script>
</body>
<script type="text/javascript">
// 'solution code' goes here...
</script>
'solution code':
$(document).ready(function(){
// HIDE ELEMENTS UPON PAGE LOAD (CHECKBOX = UNCHECKED)
if ($([id="show_box"]).is(':checked') == false)
$("#hiddenDiv").hide();
// SHOW/HIDE ELEMENTS VIA CHECKBOX STATUS
$('[id="show_box"]').change(function()
{
if ($(this).is(':checked'))
$("#hiddenDiv").fadeIn();
else
$("#hiddenDiv").fadeOut();
});
});
- [id="show_box"] (the checkbox, with id 'show_box')
- #hiddenDiv (the id of the element I want to hide/show)
- this (since it's nested, it ends up representing 'show_box')
And here is the HTML code on the page, which the 'solution code' is referring to:
<!-- CHECKBOX -->
<input type="checkbox" id="show_box" name="box" value="">
<!-- ELEMENT THAT SHOWS/HIDES -->
<div id="hiddenDiv"></div>
You can use the method which returns true if an element is checked, otherwise it returns false..is(':checked')
Example
$('input').on('click', function(){
isChecked = $(this).is(':checked')
if(isChecked){
$('html').css('background-color','green')
}
else{
$('html').removeAttr('style')
}
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<label><input type="checkbox"> Try me </label>
I needed to know this property to automatically check/uncheck "sub-checkboxes" and this is working :
jQuery('.checkbox-parent').click(function(){
jQuery('.checkbox-child').prop(
'checked',
jQuery('.checkbox-parent').prop('checked')
);
});
Following can be done in both JavaScript and jQuery which is pretty simple.
var getVal=$('#isAgeSelected').is(":checked"); // jQuery
var getVal=document.getElementById("isAgeSelected").checked //JavaScript
if (getVal==true) {
// do something
} else {
// do something
}
评论
$('input.checkbox_check').checked
如果选中,则返回,否则返回true
false
checked
$('input.checkbox_check')[0].checked
checked: function(){return this.is(:checked)}