[name^="value"]

选择指定属性是以给定字符串开始的元素

jQuery("[attribute^='value']")

attribute: 一个属性名.

value: 一个属性值,可以是一个不带引号的一个单词或带一个引号的字符串。

这个选择器能很方便的定位一些由服务器端框架生成的语义化的 ID,它们可能带有相同的前缀。然而这个选择器的速度要比用 class 选择器慢得多。所以如果可能的话,最好在这些元素上生成相同的 class,之后使用 class 选择器来选中它们。

例子

Finds all inputs with an attribute name that starts with 'news' and puts text in them.

<!DOCTYPE html>
<html>
<head>
  <script src="./static/js/jquery-3.5.0.js"></script>
</head>
<body>
  <input name="newsletter" />
 
  <input name="milkman" />
  <input name="newsboy" />
<script>$('input[name^="news"]').val('news here!');</script>
 
</body>
</html>

上篇: [name!="value"]

下篇: [name]