简体   繁体   English

jQuery通配符,用于在选择器中使用属性

[英]JQuery Wildcard for using atttributes in selectors

I've research this topic extensibly and I'm asking as a last resort before assuming that there is no wildcard for what I want to do. 我已经对该主题进行了广泛的研究,并且在我假设没有想要做的通配符之前,我要求做最后一招。

I need to pull up all the text input elements from the document and add it to an array. 我需要从文档中拉出所有文本输入元素,并将其添加到数组中。 However, I only want to add the input elements that have an id. 但是,我只想添加具有id的输入元素。

I know you can use the \\S* wildcard when using an id selector such as $(#\\S*), however I can't use this because I need to filter the results by text type only as well, so I searching by attribute. 我知道在使用$(#\\ S *)这样的id选择器时可以使用\\ S *通配符,但是我不能使用它,因为我还需要按文本类型过滤结果,因此我通过属性。

I currently have this: 我目前有这个:

values_inputs = $("input[type='text'][id^='a']");

This works how I want it to but it brings back only the text input elements that start with an 'a'. 这可以按我想要的方式工作,但它只带回以'a'开头的文本输入元素。 I want to get all the text input elements with an 'id' of anything. 我想获取带有任何“ id”的所有文本输入元素。 I can't use: 我不能使用:

values_inputs = $("input[type='text'][id^='']"); //or
values_inputs = $("input[type='text'][id^='*']"); //or
values_inputs = $("input[type='text'][id^='\\S*']"); //or
values_inputs = $("input[type='text'][id^=\\S*]"); 
//I either get no values returned  or a syntax error for these

I guess I'm just looking for the equivalent of * in SQL for JQuery attribute selectors. 我猜我只是在为SQL查询JQuery属性选择器寻找*的等效项。 Is there no such thing, or am I just approaching this problem the wrong way? 没有这样的事情,还是我只是以错误的方式来解决这个问题?

Your logic is a bit ambiguous. 您的逻辑有点含糊。 I believe you don't want elements with any id, but rather elements where id does not equal an empty string. 我相信你不想任何 ID其中,ID 等于空字符串元素,而是元素。 Use this. 用这个。

values_inputs = $("input[type='text']")
    .filter(function() { 
        return this.id != '';
     });

尝试将选择器更改为:

$("input[type='text'][id]")

实际上,这很简单:

var values_inputs = $("input[type=text][id]");

I figured out another way to use wild cards very simply. 我想出了一种非常简单地使用通配符的方法。 This helped me a lot so I thought I'd share it. 这对我很有帮助,所以我想分享一下。

You can use attribute wildcards in the selectors in the following way to emulate the use of '*'. 您可以通过以下方式在选择器中使用属性通配符来模拟对“ *”的使用。 Let's say you have dynamically generated form in which elements are created with the same naming convention except for dynamically changing digits representing the index: 假设您具有动态生成的形式,其中使用相同的命名约定创建元素,但动态更改表示索引的数字除外:

id='part_x_name' //where x represents a digit 

If you want to retrieve only the text input ones that have certain parts of the id name and element type you can do the following: 如果您只想检索包含id名称和元素类型的某些部分的文本输入,则可以执行以下操作:

var inputs = $("input[type='text'][id^='part_'][id$='_name']");

and voila, it will retrieve all the text input elements that have "part_" in the beginning of the id string and "_name" at the end of the string. 瞧,它将检索所有在id字符串的开头具有“ part_”而在字符串的末尾具有“ _name”的文本输入元素。 If you have something like 如果你有类似的东西

id='part_x_name_y' // again x and y representing digits

you could do: 你可以做:

var inputs = $("input[type='text'][id^='part_'][id*='_name_']"); //the *= operator means that it will retrieve this part of the string from anywhere where it appears in the string. 

Depending on what the names of other id's are it may start to get a little trickier if other element id's have similar naming conventions in your document. 如果其他元素ID在文档中具有相似的命名约定,则根据其他ID的名称,可能会变得有些棘手。 You may have to get a little more creative in specifying your wildcards. 在指定通配符时,您可能需要更多的创意。 In most common cases this will be enough to get what you need. 在大多数情况下,这足以满足您的需求。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM