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

时间:2014-01-26 21:17:42

标签: javascript jquery

我有类似的东西:

<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>

并在我的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 ?
});

3 个答案:

答案 0 :(得分:1)

您可以使用:not()

将您的选择器更改为

$('#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

答案 1 :(得分:0)

$('#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 ?
});

答案 2 :(得分:0)

您可以使用jQuery not选择器:

  

选择与给定选择器不匹配的所有元素。

代码:

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

});

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