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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 10:27:52  来源:igfitidea点击:

$("input:not(:empty)") is not working

jquery

提问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

:emptychecks for whether an element has child elements. inputelements cannot have child elements.

:empty检查元素是否有子元素。input元素不能有子元素。

If you want to test that the inputelement'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 inputelements, and then filter it so only the ones whose valueproperty 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 :emptydoes 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');
}