最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

javascript - select all inputs except hidden (but with one exception) - Stack Overflow

matteradmin4PV0评论

Lets say I have a set of inputs on a form:

<form id="myform">
  <input type="checkbox" id="goat_1">
  <input type="checkbox" id="goat_2">
  <input type="text" id="pig_3">
  <input type="hidden" id="cow_1">
  <input type="hidden" id="chick_3">
  <input type="hidden" id="duck_5">
</form>

I want to select all inputs, except type="hidden", but with one exception I DO want any hidden input with an id beginning with "duck". I need this all in one array so I can iterate through it.

So the first two parts are easy:

$("#myform").find(":input").not("[type=hidden]").each(
                                          function () { alert("do stuff"); })

But what about the exception?

I am looking for the cleanest way to do this (prefer one line/statement).

Lets say I have a set of inputs on a form:

<form id="myform">
  <input type="checkbox" id="goat_1">
  <input type="checkbox" id="goat_2">
  <input type="text" id="pig_3">
  <input type="hidden" id="cow_1">
  <input type="hidden" id="chick_3">
  <input type="hidden" id="duck_5">
</form>

I want to select all inputs, except type="hidden", but with one exception I DO want any hidden input with an id beginning with "duck". I need this all in one array so I can iterate through it.

So the first two parts are easy:

$("#myform").find(":input").not("[type=hidden]").each(
                                          function () { alert("do stuff"); })

But what about the exception?

I am looking for the cleanest way to do this (prefer one line/statement).

Share Improve this question asked Aug 22, 2013 at 1:18 EkoostikMartinEkoostikMartin 6,9113 gold badges34 silver badges63 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 7

Try

$("#myform").find(":input").not("[type=hidden]:not([id^='duck'])").each(function () {
    alert("do stuff"); 
});

Try this:

$('#myform > input').not(':hidden:not([id^=duck])').each(function (i, e) {
    alert('This is my id: ' + e.id);
});

Working example in this fiddle.

Post a comment

comment list (0)

  1. No comments so far