Jquery属性NOT包含选择器

时间:2017-09-27 08:14:03

标签: jquery

这是否有负面选择器? https://api.jquery.com/attribute-contains-selector/

例如,查询属性名称没有子字符串人的所有输入

我尝试了显而易见的但没有工作

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>attributeContains demo</title>
  <script src="https://code.jquery.com/jquery-1.10.2.js"></script>
</head>
<body>

<input name="man-news">
<input name="milkman">
<input name="letterman2">
<input name="newmilk">

<script>
$( "input[name*!='man']" ).val( "has man in it!" );
</script>

</body>
</html>
  

未捕获错误:语法错误,无法识别的表达式:

这个非常接近https://api.jquery.com/attribute-not-equal-selector/但它不相等,我想不包含。

这一个jQuery "not contains" selector无效,因为我没有文本值,创建表单的方式是值在value属性中。

1 个答案:

答案 0 :(得分:2)

使用:not()

选择所有不输入的输入(名称中包含&#39; man&#39;)

&#13;
&#13;
$( "input:not([name*='man'])" ).val( "DOESN'T have man in it!" );
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input name="man-news">
<input name="milkman">
<input name="letterman2">
<input name="newmilk">
&#13;
&#13;
&#13;