我想从下拉列表中选择其他选项时激活文本框,否则它将保持不活动状态

时间:2014-09-02 06:29:48

标签: javascript html5

当我选择选项"其他"从下拉列表然后只有文本框是活动的,否则该框保持不活动...我附加代码请帮助我,并根据我的要求更改代码.......

<div class="width-qrtr">
   <label>Type of event</label>
  <select name="" id= "select">
      <option value="1">Select Type</option> 
      <option value="2">Golf Day</option>
      <option value="3">Dinner</option>
      <option value="4">Dinner and Golf Day</option>
      <option value="5">Other</option>
 </select>
  </div>
<div class="width-qrtr">
   <label>If you choose 'Other'</label>
    <input type="text"   value=""  name=""/>
</div>

4 个答案:

答案 0 :(得分:2)

以下是使用纯javascript进行操作的方法......

<select name="" id= "select" onchange="onSelectChange()">
      <option value="1">Select Type</option> 
      <option value="2">Golf Day</option>
      <option value="3">Dinner</option>
      <option value="4">Dinner and Golf Day</option>
      <option value="5">Other</option>
</select>
<input type="text" id="yourTextBox" disabled="disabled"/>

然后在你的剧本中:

function onSelectChange(){
    var sel = document.getElementById('select');
    var strUser = sel.options[sel.selectedIndex].text;  //getting the selected option's text

    if(strUser == 'Other'){ 
         document.getElementById('yourTextBox').disabled = false;  //enabling the text box because user selected 'Other' option.
    }
}

答案 1 :(得分:0)

    <div class="width-qrtr">
    <label>Type of event</label>
    <select name="" id="select">
        <option value="1">Select Type</option>
        <option value="2">Golf Day</option>
        <option value="3">Dinner</option>
        <option value="4">Dinner and Golf Day</option>
        <option value="5">Other</option>
    </select>
</div>

<input type='text' name='othertext' id="other" disabled='disabled'/>

脚本 -

$(document).ready(function () {
    $("#select").change(function () {
        if ($(this).find("option:selected").val() == "5") {
            $("#other").removeAttr("disabled")
        } else {
            $("#other").attr("disabled","disabled")
        }
    });
});

这是小提琴http://jsfiddle.net/vikrant47/gywg9hue/1/

答案 2 :(得分:0)

<div class="width-qrtr">
   <label>Type of event</label>
  <select name="selectdrop" id= "selectdrop" onchange="return Sbox();">
      <option value="1">Select Type</option> 
      <option value="2">Golf Day</option>
      <option value="3">Dinner</option>
      <option value="4">Dinner and Golf Day</option>
      <option value="5">Other</option>
 </select>
    <input type="text" id="tbox" name="tbox" disabled />
  </div>
<script lanaguage="javascript">
    function Sbox()
    {
    if(document.getElementById("selectdrop").value=='5')
    {

        document.getElementById("tbox").disabled=false;

    }
    }
</script>

正在运行代码:http://jsfiddle.net/cvb82wtq/

答案 3 :(得分:0)

将事件监听器绑定到下拉菜单。当它改变时,如果选择的选项是&#34;其他&#34; (值= 5),启用文本框。否则,请禁用文本框。

HTML:

<div class="width-qrtr">
   <label>Type of event</label>
  <select name="" id= "select">
      <option value="1">Select Type</option> 
      <option value="2">Golf Day</option>
      <option value="3">Dinner</option>
      <option value="4">Dinner and Golf Day</option>
      <option value="5">Other</option>
 </select>
  </div>
<div class="width-qrtr">
   <label>If you choose 'Other'</label>
    <input id="textbox" disabled="true" type="text"   value=""  name=""/>
</div>

JS:

document.getElementById('select').addEventListener('change', function() {
    if (this.value == 5) {
        document.getElementById('textbox').disabled = false;   
    } else {
        document.getElementById('textbox').disabled = true;
    }
});

这里是fiddle