jQuery $("input:not(:empty)") 不工作
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9173533/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
$("input:not(:empty)") is not working
提问by user1194147
<html>
<head>
<script type="text/javascript" src="jquery-1.7.1.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$("button").click(function(){
alert('hi'+$("input:text").val());
$("input:not(:empty)").val("sdfdf");
});
});
</script>
</head>
<body>
<input type="text" value="aa" />
<input type="text" value="" />
<input type="text" value="aa" />
<button>Click me</button>
</body>
</html>
i am trying to access empty textboxex using jquery and assigning a value hello to it.. but it's not working .
我正在尝试使用 jquery 访问空的 textboxex 并为其分配一个值 hello .. 但它不起作用。
thanks in advance
提前致谢
回答by T.J. Crowder
:empty
checks for whether an element has child elements. input
elements cannot have child elements.
:empty
检查元素是否有子元素。input
元素不能有子元素。
If you want to test that the input
element's value is blank:
如果要测试input
元素的值是否为空:
$(document).ready(function(){
$("button").click(function(){
$("input").filter(function() {
return this.value.length !== 0;
}).val("sdfdf");
});
});
There we get all of the input
elements, and then filter it so only the ones whose value
property isn't ""
are included.
在那里我们获取所有input
元素,然后对其进行过滤,以便只包含value
属性不""
包含的元素。
回答by Candide
You could use another selector for your task:
您可以为您的任务使用另一个选择器:
$("input[value='']").val("sdfdf");
回答by Znarkus
jQuery(':empty') Description: Select all elements that have no children (including text nodes).
jQuery(':empty') 描述:选择所有没有子元素的元素(包括文本节点)。
From http://api.jquery.com/empty-selector/. Thus :empty
does not do what you think it does.
来自http://api.jquery.com/empty-selector/。因此:empty
不会做你认为它会做的事情。
Have a look at this answer https://stackoverflow.com/a/1299468/138023for a solution.
看看这个答案https://stackoverflow.com/a/1299468/138023以获得解决方案。
回答by user909410
You could use an .each with this inside:
你可以在里面使用 .each :
if( $(this).val().length === 0 ) {
$(this).parents('p').addClass('warning');
}