2011-10-17 25 views
0

我正在嘗試創建匹配/不匹配警報的jQuery。基本上我有一個nTier系列的下拉和輸入字段。我該如何創建一個jQuery nTier select比較腳本?

如果用戶下拉選擇並選擇一個值,並且該值與另一個選擇組下拉下來。然後我需要去比較價格和perCase值,並確保有匹配。

如果價格不匹配,我需要生成警報......如果案例不匹配,我需要生成警報。

我可以做兩個,但我需要這個來聚合並堅持nTier數量的select/price/case組,我對如何做到這一點感到困惑。

這是我正在處理的清理簡化表單。

<form name="form1" ID="form1"> 
<table> 
<tr> 
<td> 
    <select name="selectA"> 
     <option id="A" value="">None</option> 
     <option id="A" value="A">A</option> 
     <option id="A" value="B">B</option> 
     <option id="A" value="C">C</option> 
    </select> 
</td> 
<td> 
    <input id="priceA" type="text" name="price" value="8.99"> 
</td> 
<td> 
    <input id="perCaseA" type="text" name="perCase" value="4"> 
</td> 
</tr> 
<tr> 
<td> 
    <select name="selectB"> 
     <option id="B" value="">None</option> 
     <option id="B" value="A">A</option> 
     <option id="B" value="B">B</option> 
     <option id="B" value="C">C</option> 
    </select> 
</td> 
<td> 
    <input id="priceB" type="text" name="price" value="8.99"> 
</td> 
<td> 
    <input id="perCaseB" type="text" name="perCase" value="4"> 
</td> 
</tr> 
</table> 
</form> 

回答

1

我確信這應該是可能的只是使用jQuery選擇器,但不完全。呃,好吧。 :)

此選擇所有select元件,然後靠對檢查它們的值過濾然後觸發select(顯然xxx[value="xx"]不適用於select工作)。然後過濾任何匹配select s(價格文本框)後的input,以確保它不具有與觸發select的以下input相同的值。如果你仍然得到匹配,你有一個不一致,需要一個警報。

不幸的是因爲該表格而需要使用parent().next().children()

你將需要一個類似的代碼位的情況下,嗯,大小寫。我不認爲你可以同時檢查兩個不幸的。

$("select").change(function() 
{ 
    var $this = $(this); 

    if ($("select").filter(function() { return $(this).val() == $this.val(); }).parent().next().children("input[value!='" + $this.parent().next().children("input").val() + "']").length != 0) 
    { 
     // alert 
    } 
}); 
+0

好吧,讓我開始謝謝你。 – 2011-10-17 15:11:48

相關問題