2008-11-09 39 views
14

我有這樣的形式:選擇使用jQuery多的後裔?

<form name="customize"> 
    Only show results within 
     <select name="distance" id="slct_distance"> 
      <option>25</option> 
      <option>50</option> 
      <option>100</option> 
      <option value="10000" selected="selected">Any</option> 
    </select> miles of zip code 
    <input type="text" class="text" name="zip_code" id="txt_zip_code" /> 
    <span id="customize_validation_msg"></span> 
</form> 

我怎樣才能選擇inputselect一個jQuery選擇?

我試過,但它選擇的所有頁面上的選擇和投入:

$("form[name='customize'] select,input") 

回答

32

在選擇字符串中的逗號分隔完全獨立的表情,就像在CSS,所以你給的選擇得到窗體上名爲「定製」的形式中選擇元素和所有輸入(如你所描述)。這聽起來像你想是這樣的:

$("form[name='customize'] select, form[name='customize'] input") 

,或者如果你沒有進入repitition,這樣的:

$("form[name='customize']").children("select, input") 
+0

啊我明白了。謝謝! – Greg 2008-11-09 18:42:38

0

對我來說,你的建議的工作。您也可以使用

form[name='customize'] select, form[name='customize'] input 

這兩個選擇器都按我的看法工作。也許問題在別的地方?

我試圖

$("form[name='customize'] select, input").css('font-size', '80px'); 

您例如HTML。選擇和輸入的字體大小已更改。

---編輯---

我上面的建議是正確的。它只選擇自定義表單中的元素。

+0

這工作,而選擇所有輸入和選擇的頁面上,所以如果你在頁面上測試與只是形式,將工作,但不是真實的頁面。我澄清了我的問題。 – Greg 2008-11-09 18:34:01

6

較短的語法$(選擇,parentselector)也是可能的。 例如,此網頁上:

// all spans 
$("span").css("background-color","#ff0"); 

// spans below a post-text class 
$("span", ".post-text").css("background-color","#f00"); 

編輯 - 我忘了幾種兒童的具體情況!

// spans and p's below a post-text class 
$("span,p", ".post-text").css("background-color","#f00");