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

I want to activate a textbox on selecting other option from dropdown list only else it remains inactive

本文关键字:文本 一个 活动状态 下拉列表 选择 其他 激活 选项      更新时间:2023-09-26

我希望当我从下拉列表中选择选项"others"时,只有文本框处于活动状态,否则该框保持不活动状态。。。我正在附上代码,请帮助我,并根据我所需的规范更改代码。。。。。。。

<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>

以下是如何使用纯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.
    }
}
<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/

    <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/

将事件侦听器绑定到下拉菜单。更改时,如果选择的选项为"其他"(值=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;
    }
});

这是一把小提琴。