如果我有一个输入:
<input type="checkbox" name="product" id="product" /><label for="product">Product</label>
如何判断何时使用 $(this) 检查它?
$('input#product').click(function() {
console.log('clicked or not clicked?');
});
最佳答案
使用元素属性:
this.checked
使用jQuery.prop()
:
$(this).prop('checked');
<小时/>
来自 jQuery 文档(关于 $.prop()
):
The preferred cross-browser-compatible way to determine if a checkbox is checked is to check for a "truthy" value on the element's property using one of the following:
if ( elem.checked )
if ( $(elem).prop("checked") )
if ( $(elem).is(":checked") )
关于$(this) 上的 jquery 选择器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8312591/