我在文档使用通配符或正则表达式(不确定的确切术语)与jQuery选择器。

我自己也找过,但一直无法找到关于语法和如何使用它的信息。有人知道语法的文档在哪里吗?

编辑:属性过滤器允许您根据属性值的模式进行选择。


当前回答

这些都是有帮助的。

如果你是通过Contains找到的,那么它会是这样的

    $("input[id*='DiscountType']").each(function (i, el) {
         //It'll be an array of elements
     });

如果你用start With来找,它会是这样的

    $("input[id^='DiscountType']").each(function (i, el) {
         //It'll be an array of elements
     });

如果你用Ends With来找,它会是这样的

     $("input[id$='DiscountType']").each(function (i, el) {
         //It'll be an array of elements
     });

如果你想选择id不是给定字符串的元素

    $("input[id!='DiscountType']").each(function (i, el) {
         //It'll be an array of elements
     });

如果要选择名称包含用空格分隔的给定单词的元素

     $("input[name~='DiscountType']").each(function (i, el) {
         //It'll be an array of elements
     });

如果要选择id等于给定字符串或以该字符串开头加连字符的元素

     $("input[id|='DiscountType']").each(function (i, el) {
         //It'll be an array of elements
     });

其他回答

var test = $('#id').attr('value').match(/[^a-z0-9 ]+/);

给你!

id和类仍然是属性,因此如果进行相应的选择,可以对它们应用regexp属性筛选器。点击此处阅读更多信息: http://rosshawkins.net/archive/2011/10/14/jquery-wildcard-selectors-some-simple-examples.aspx

如果正则表达式的使用仅限于测试属性是否以特定字符串开头,可以使用^ JQuery选择器。

例如,如果你只想选择id以“abc”开头的div,你可以使用:

$("div[id^='abc']")

可以在这里找到很多非常有用的选择器来避免使用regex: http://api.jquery.com/category/selectors/attribute-selectors/

$("input[name='option[colour]'] :checked ")

这些都是有帮助的。

如果你是通过Contains找到的,那么它会是这样的

    $("input[id*='DiscountType']").each(function (i, el) {
         //It'll be an array of elements
     });

如果你用start With来找,它会是这样的

    $("input[id^='DiscountType']").each(function (i, el) {
         //It'll be an array of elements
     });

如果你用Ends With来找,它会是这样的

     $("input[id$='DiscountType']").each(function (i, el) {
         //It'll be an array of elements
     });

如果你想选择id不是给定字符串的元素

    $("input[id!='DiscountType']").each(function (i, el) {
         //It'll be an array of elements
     });

如果要选择名称包含用空格分隔的给定单词的元素

     $("input[name~='DiscountType']").each(function (i, el) {
         //It'll be an array of elements
     });

如果要选择id等于给定字符串或以该字符串开头加连字符的元素

     $("input[id|='DiscountType']").each(function (i, el) {
         //It'll be an array of elements
     });