使用jquery选择器和正则表达式查找元素

时间:2013-10-02 10:31:27

标签: jquery regex jquery-selectors

我有几个输入框如下:

<input type="checkbox" name="checkbox_item[500]" />
<input type="checkbox" name="checkbox_item[10]" />
<input type="checkbox" name="checkbox_item[2]" />
<input type="checkbox" name="checkbox_item[1]" />
<input type="checkbox" name="checkbox_item[]" />

所以我使用了以下正则表达式模式checkbox_item\[\d+\],它在我的正则表达式测试器中运行良好,但是我无法使用Jquery。这是我的Jquery代码:

console.log($("input:regex(name, /checkbox_item\[\d+\]/)").length);

我期待4而不是28!

有人可以对此有所了解吗?

由于

3 个答案:

答案 0 :(得分:1)

我认为jQuery本身并不支持选择器中的正则表达式。

但您可以使用Attribute Starts With Selector [name^="value"]

答案 1 :(得分:1)

您可以将Attribute Starts

一起使用
console.log($("input[type=checkbox][name^='checkbox_item']").length)

Demo

答案 2 :(得分:1)

我不知道我是否理解它,但你想获得以checkbox_item开头的总元素?

$("input[name^=checkbox_item]").length

这将为您提供以checkbox_item开头的总元素。

有关此选择器的更多信息here

相关问题