我在使用 jQuery 的 :empty 伪选择器时遇到问题。基本上,它没有按照我认为应该的方式处理输入。鉴于此 html,我试图计算所有没有循环的值的输入。
<div id="sampleDiv">
<label>My first Input:</label>
<input name="test" id="firstInput" type="text" value="default text" /><br/><br/>
<label>My second Input:</label>
<input name="test" id="secondInput" type="text" value="" /><br/><br/>
<label>My third Input:</label>
<input name="test" id="thirdInput" type="text" value="" />
我正在使用这样的选择器
$('#sampleDiv input:empty').length
但它返回 3,包括具有值的那个。这是 fiddle ,感谢您的帮助!: https://jsfiddle.net/chas688/b5fad3Lu/
最佳答案
:empty
选择器旨在查看元素的子元素。这就是为什么在这种情况下它不一定对您有用。相反,您可以通过 value
属性进行选择:
$('#sampleDiv input[value=""]').length;
或者通过filter()
:
$('#sampleDiv input').filter(function() {
return this.value == '';
}).length;
关于javascript - jQuery :empty not working with input,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31007388/