2011-09-08 49 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";  
    } 
}