选择选项时jQuery显示文本框

时间:2010-03-24 11:34:43

标签: javascript jquery hide show

我有以下代码:

<select>
<option value="Type 1">Type 1</option>
<option value="Type 2">Type 2</option>
<option value="Type 3">Type 3</option>
<option value="Other">Other</option>
</select>

<input type="text" id="other" />

我想要做的是使用jQuery默认隐藏下面的文本框,然后在用户从下拉列表中选择其他选项时显示它。

4 个答案:

答案 0 :(得分:11)

这里不需要任何CSS。

$('#sel').change(function() {
    var selected = $(this).val();
    if(selected == 'Other'){
      $('#other').show();
    }
    else{
      $('#other').hide();
    }
});

答案 1 :(得分:7)

<select id="sel">
<option value="Type 1">Type 1</option>
<option value="Type 2">Type 2</option>
<option value="Type 3">Type 3</option>
<option value="Other">Other</option>
</select>

<input type="text" id="other" style="display: none;" />

$('#sel').change(function() {
    $('#other').css('display', ($(this).val() == 'Other') ? 'block' : 'none');
});

答案 2 :(得分:0)

试试这个:

<select id="selectbox_id">
<option value="Type 1">Type 1</option>
<option value="Type 2">Type 2</option>
<option value="Type 3">Type 3</option>
<option value="Other">Other</option>
</select>

<input type="text" id="other" />

<强> JQuery的:

$(function(){
  // hide by default
  $('other').css('display', 'none');

  $('selectbox_id').change(function(){
   if ($(this).val() === 'Other') {
     $('other').css('display', 'block');
   }
 });
});

答案 3 :(得分:0)

<select id = "ddlPassport" onchange = "ShowHideDiv()" style="width:150px;">
<option value="Y">Type 1</option>
<option value="Y">Type 2</option>
<option value="N">Type 3</option>
<option value="D">Other</option>
</select>


<script type="text/javascript">
    function ShowHideDiv() 
    {
    var ddlPassport = document.getElementById("ddlPassport");
    var dvPassport = document.getElementById("dvPassport");
    dvPassport.style.display = ddlPassport.value == "Y" ? "block" : "none";
    dvPassport1.style.display = ddlPassport.value == "N" ? "block" : "none";
    dvPassport2.style.display = ddlPassport.value == "D" ? "block" : "none";

    }



 <div id="dvPassport" style="display: none">
          <form action="viewdata.php" method="POST">
        Enter Data:
        <input type="text" id="txtPassportNumber" name="data" />
        <br>
        <input type="submit" value="Search" name="submit" 
     style="float:right;width:150px;margin-top:10px;">
    </form>
    </div>
    <div id="dvPassport1" style="display: none">
        <form action="viewdata.php" method="POST">
        Enter Data:
        <input type="number" id="txtPassportNumber" name="data"  />
         <br>
        <input type="submit" value="Search" name="submit" 
   style="float:right;width:150px;margin-top:10px;">
    </form>
    </div>
    <div id="dvPassport2" style="display: none">
        <form action="viewdata.php" method="POST">
        Enter Data:
        <input type="date" id="txtPassportNumber" name="data"  />
         <br>
        <input type="submit" value="Search" name="submit" 
       style="float:right;width:150px;margin-top:10px;">
    </form>
    </div>