简体   繁体   English

jQuery:为给定名称选择所有INPUT,SELECT元素,但排除隐藏类型

[英]jQuery: Select all INPUT, SELECT elements for given name but exclude hidden types

I got something like that: 我得到这样的东西:

<form id="my_form">
<input type="hidden" name="name_1" value="1">
<input type="text" name="name_2" value="text_1">
<input type="text" name="name_3" value="text_2">
<select name="name_4">
    <option value="1">opt1</option>
    <option value="2">opt2</option>
</select>
</form>

and in my jQuery code: 在我的jQuery代码中:

$('#my_form').serializeArray().forEach(function(item){
  // here 'item' has two props: 'name' and 'value'
  // how can I select form's item based on name but NOT type HIDDEN ?
});

You can use :not() 您可以使用:not()

Change Your selector as 将您的选择器更改为

$('#my_form :not(:hidden)').serializeArray().forEach(function(item){
  // here 'item' has two props: 'name' and 'value'
  // how can I select form's item based on name but NOT type HIDDEN ?
});

DEMO DEMO

$('#my_form').find("input[type!='hidden']").serializeArray().forEach(function(item){
  // here 'item' has two props: 'name' and 'value'
  // how can I select form's item based on name but NOT type HIDDEN ?
});

You can use jQuery not selector: 您可以使用jQuery not选择器:

Selects all elements that do not match the given selector. 选择与给定选择器不匹配的所有元素。

Code: 码:

$('#my_form :not([type=hidden])').serializeArray().forEach(function(item){

});

Demo: http://jsfiddle.net/5gygs/ 演示: http//jsfiddle.net/5gygs/

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

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