$(this) 上的 jquery 选择器

标签 jquery this

如果我有一个输入:

<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/

相关文章:

jQuery - $.each 查找是否第一次运行 $.each

javascript - 如何使用传递给乐趣的对象作为内部函数的上下文?

javascript - 为什么 'this.value' 有效但 e.value 无效?

javascript - JavaScript 中的“this”引用

jQuery 此选择器不可用于 animate() 方法属性

javascript - Jquery 尝试使用 html() 获取 src

jQuery fadeIn/fadeOut IE cleartype 故障如何修复过渡

javascript - 为什么这个 jQuery cookie 没有被检索到?

javascript - "this"返回函数而不是对象

javascript - Bootstrap 和 Chosen 插件之间的冲突