2011-03-14 84 views
0

我將一些工作完美無瑕但卻非常醜陋的代碼入侵了,我想學會清理它。根據jQuery中的單選按鈕選擇的數量顯示/隱藏1-5個輸入框需要清理

有沒有人有更好的建議,那麼這種方法?我使用了一些屬性選擇器來減少重複,但是我仍然需要爲每個硬件設置一個大塊(現在有4個)。我想我可以使用硬件變量的循環?

$('input[name="hwA-qty"]').click(function(){ 
    var selected = $(this).val(); 
    if (selected == 1) { //Hide inputs 2-5 
     $('fieldset[id^="hwA"]').hide(); 
    } 
    if (selected == 2) { //Show inputs 1 & 2 
     $('fieldset#hwA-2').show(); 
     $('fieldset#hwA-3').hide(); 
     $('fieldset#hwA-4').hide(); 
     $('fieldset#hwA-5').hide(); 
    } 
    if (selected == 3) { //Show inputs 1-3 
     $('fieldset#hwA-2').show(); 
     $('fieldset#hwA-3').show(); 
     $('fieldset#hwA-4').hide(); 
     $('fieldset#hwA-5').hide(); 
    } 
    if (selected == 4) { //Show inputs 1-4 
     $('fieldset#hwA-2').show(); 
     $('fieldset#hwA-3').show(); 
     $('fieldset#hwA-4').show(); 
     $('fieldset#hwA-5').hide(); 
    } 
    if (selected == 5) { //Show all inputs 
     $('fieldset[id^="hwA"]').show(); 
    } 
}); 
$('input[name="hwB-qty"]').click(function(){ 
    var selected = $(this).val(); 
    if (selected == 1) { 
     $('fieldset[id^="hwB"]').hide(); 
    } 
    if (selected == 2) { 
     $('fieldset#hwB-2').show(); 
     $('fieldset#hwB-3').hide(); 
     $('fieldset#hwB-4').hide(); 
     $('fieldset#hwB-5').hide(); 
    } 
    if (selected == 3) { 
     $('fieldset#hdPvr2').show(); 
     $('fieldset#hdPvr3').show(); 
     $('fieldset#hdPvr4').hide(); 
     $('fieldset#hdPvr5').hide(); 
    } 
    if (selected == 4) { 
     $('fieldset#hdPvr2').show(); 
     $('fieldset#hdPvr3').show(); 
     $('fieldset#hdPvr4').show(); 
     $('fieldset#hdPvr5').hide(); 
    } 
    if (selected == 5) { 
     $('fieldset[id^="sdPvr"]').show(); 
    } 
}); [...] 

而這裏的HTML:

[...] 
<div class="hardwareValidation"> 

<div class="select"> 
    <label for="hwA-qty">Quantity</label><br /> 
    <label>1 <input type="radio" name="hwA-qty" value="1" checked /></label> 
    <label>2 <input type="radio" name="hwA-qty" value="2" /></label> 
    <label>3 <input type="radio" name="hwA-qty" value="3" /></label> 
    <label>4 <input type="radio" name="hwA-qty" value="4" /></label> 
    <label>5 <input type="radio" name="hwA-qty" value="5" /></label> 
</div> 

<fieldset id="hwA-1"> 
    <div class="input"> 
     <label for="hwA-1-serial">#1 - Box Serial Number</label><br /> 
     <input type="text" name="hwA-1-serial" id="hwA-1-serial" /> 
    </div> 
    <div class="checkbox"> 
     <label> 
      <input type="checkbox" name="hwA-1-override" value=""> Override Validation 
     </label> 
    </div> 
</fieldset>      

<fieldset id="hwA-2"> 
    <div class="input"> 
     <label for="hwA-2-serial">#2 - Box Serial Number</label><br /> 
     <input type="text" name="hwA-2-serial" id="hwA-2-serial" /> 
    </div> 
    <div class="checkbox"> 
     <label> 
      <input type="checkbox" name="hwA-2-override" value=""> Override Validation 
     </label> 
    </div> 
</fieldset> 

</div> [...] 
+0

你也可以考慮使用jQuery根據需要動態地添加儘可能多的輸入字段。如果你不使用它們,它們不需要存在於HTML標記中。 – 2011-03-14 14:19:03

回答

1
$('input[name="hwA-qty"]').click(function(){ 
var selected = $(this).val(); 

for(i=1;i<=5;i++){ 
    if(i<=selected)$('fieldset#hwA-'+i).show(); 
    else $('fieldset#hwA-'+i).hide(); 
} 

}

+0

謝謝,這太好了。但它只隱藏了最後的元素。 所以,如果我選擇「5」,5輸入顯示。然後我點擊「2」,只有最後一個輸入被刪除,所以剩下4個。我想我需要添加條件來隱藏輸入。 – lazyronin 2011-03-14 13:54:24

+0

似乎爲我工作... http://jsfiddle.net/PH7vM/ – 2011-03-14 14:16:16

+0

哇我的壞,犯了一個錯字。非常感謝! – lazyronin 2011-03-14 14:19:08

0

你可以使用nextAll穿越方法,即選擇匹配的元素下面的兄弟姐妹。

$('input[name="hwA-qty"]').click(function(){ 
     var selected = $(this).val(); 
     $("fieldset[id^='hwA-']").show(); 
     $("#hwA-" + selected).nextAll().hide(); 
    }); 

$('input[name="hwA-qty"]:checked').trigger("click"); // Triggers the click event to set to the initial position 

參見:http://jsfiddle.net/GlauberRocha/s4PAG/1/

+0

謝謝,我會研究這種方法。以前沒有聽說過。 – lazyronin 2011-03-14 13:57:48

相關問題