简体   繁体   English

Jquery选择器输入[type = text]')

[英]Jquery selector input[type=text]')

I wrote a code that basically selects all input type=text element like this: 我编写了一个基本上选择所有input type=text元素的代码,如下所示:

$('.sys input[type=text]').each(function () {}

How do I change it to select input[type=text] or select ? 如何更改它以选择input[type=text]select

Using a normal css selector: 使用普通的css选择器:

$('.sys input[type=text], .sys select').each(function() {...})

If you don't like the repetition: 如果你不喜欢重复:

$('.sys').find('input[type=text],select').each(function() {...})

Or more concisely, pass in the context argument: 或者更简洁地传递context参数:

$('input[type=text],select', '.sys').each(function() {...})

Note: Internally jQuery will convert the above to find() equivalent 注意:内部jQuery会将上面的内容转换为find()等效

http://api.jquery.com/jQuery/ http://api.jquery.com/jQuery/

Internally, selector context is implemented with the .find() method, so $('span', this) is equivalent to $(this).find('span'). 在内部,选择器上下文是使用.find()方法实现的,因此$('span',this)等价于$(this).find('span')。

I personally find the first alternative to be the most readable :), your take though 我个人发现第一个替代方案是最可读的:),你的看法

$('.sys').children('input[type=text], select').each(function () { ... });

EDIT: Actually this code above is equivalent to the children selector .sys > input[type=text] if you want the descendant select ( .sys input[type=text] ) you need to use the options given by @NiftyDude. 编辑:实际上这个代码相当于子选择器.sys > input[type=text]如果你想要后代选择( .sys input[type=text] )你需要使用@NiftyDude给出的选项。

More information: 更多信息:

If you have multiple inputs as text in a form or a table that you need to iterate through, I did this: 如果您需要迭代的表单或表格中有多个输入作为文本,我这样做:

var $list = $("#tableOrForm :input[type='text']");

$list.each(function(){
    // Go on with your code.
});

What I did was I checked each input to see if the type is set to "text", then it'll grab that element and store it in the jQuery list. 我做的是检查每个输入以查看类型是否设置为“text”,然后它将获取该元素并将其存储在jQuery列表中。 Then, it would iterate through that list. 然后,它将遍历该列表。 You can set a temp variable for the current iteration like this: 您可以为当前迭代设置临时变量,如下所示:

var $currentItem = $(this);

This will set the current item to the current iteration of your for each loop. 这会将当前项设置为每个循环的当前迭代。 Then you can do whatever you want with the temp variable. 然后你可以用temp变量做任何你想做的事。

Hope this helps anyone! 希望这有助于任何人!

$('input[type=text],select', '.sys');

for looping: 用于循环:

$('input[type=text],select', '.sys').each(function() {
   // code
});

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

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