2011-09-08 48 views
1

基本上,我想基於下拉菜單的選定值來隱藏輸入選項。具體來說,如果選擇的值是tid和acc,我只顯示這個輸入標籤。Javascript:根據特定下拉菜單的值隱藏表單輸入

我有這個下拉菜單。

<select id="select1742855Rule Type" onchange="document.getElementById('form1742855_Rule Type').value=this.value;"> 
     <option value="" selected="selected">None</option> 
     <option value="tid">tid</option> 
     <option value="tid and acc">tid and acc</option> 
     <option value="xid">xid</option> 
</select> 

而這種輸入標籤:

<input type="text" size="6" id = "acc" onchange="document.getElementById('form1742851_Id').value=this.value;"> 

我是一個徹底的新手,當涉及到的JavaScript。任何幫助都感激不盡!

+1

FYI空格在ID中不是有效字符。 (你的選擇「); –

+0

你能否請,粘貼代碼終於工作:) –

回答

2

首先:做對照沒有用空格id

$('#select1742855Rule_Type').change(function() { 
    var val = $(this).val(); 
    if (val == 'tid and acc') { 
     $('#acc').show(); 
    } 
    else { 
     $('#acc').hide(); 
    } 
}); 
+0

我在這裏問了一個後續問題:http://stackoverflow.com/questions/7353403/ JavaScript的隱藏-A-形式輸入-取決於 - 上的值對的一 - 特定 - 下拉 – Spencer

0
$(function() { 
    $("yourInput").hide(); 
    $("yourSelect").change(function() { 
     if ($(this).val() == "yourVal") 
      $("yourInput").show(); 
     else 
      $("yourInput").hide(); 
    }); 
}); 
+0

試過這段代碼,但沒有工作: <選擇ID = 「yourSelect」> <選項值= 「」 選擇= 「選擇」>無 <選項值= 「TID」> TID <選項值= 「yourVal」> yourVal <選項value =「xid」> xid

0

嘗試是這樣的:

注意:從ID刪除的空間。

toggleInput = function(){  
    var select = document.getElementById("form1742855_Rule_Type"); 
    if (select){ 
     document.getElementById("acc").style.display = select.options[select.selectedIndex].value == "tid and acc" ? "block" : "none";  
    } 
}