Javascript Jquery 选择器输入[type=text]')
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10649869/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Jquery selector input[type=text]')
提问by Hyman
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
?
如何将其更改为 selectinput[type=text]
或select
?
回答by Andreas Wong
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()
等效
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
我个人认为第一个选择是最易读的:),不过你的看法
回答by Wouter J
$('.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:
更多信息:
回答by Jason Cidras
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. Then, it would iterate through that list. You can set a temp variable for the current iteration like this:
我所做的是检查每个输入以查看类型是否设置为“文本”,然后它会获取该元素并将其存储在 jQuery 列表中。然后,它将遍历该列表。您可以为当前迭代设置一个临时变量,如下所示:
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.
这会将当前项目设置为每个循环的当前迭代。然后你可以对临时变量做任何你想做的事情。
Hope this helps anyone!
希望这可以帮助任何人!
回答by thecodeparadox
$('input[type=text],select', '.sys');
for looping:
用于循环:
$('input[type=text],select', '.sys').each(function() {
// code
});