繁体   English   中英

如果用户在从数据库检索到的下拉菜单中选择“其他”,则显示一个文本框

[英]display a textbox if user select “Other” on the drop down menu retrieved from the database

请帮我这个。 如果用户在下拉菜单中选择“其他”选项,则无法显示文本框。 下拉列表中的值来自数据库。 谢谢! :)

 <th align="left" height="26" colspan="3"><strong>Return Reason Codes:</strong><span class="select">
      <?php

            echo "<select description='returnreason' type='text' id='returnreason' onchange=' showfield(this.options[this.selectedIndex].value)'>";
            echo '<option id="0">'.'    --Select Return Reason--  '.'</option>';
            $sql = mysql_query("SELECT * FROM preturnreason");
            while($record = mysql_fetch_assoc($sql)) 
                   {
                    echo '<option value=" '.$record['DESCRIPTION'].'">'.$record['DESCRIPTION']. '</option>';

                   }        
            echo '</select>';

         ?> <div id="div1"></div></th>
    </tr>

这是我的JavaScript:

<script type="text/javascript">
function showfield(name){
  if(name=='Other')document.getElementById('div1').innerHTML='Other: <input type="text" name="other" />';
  else document.getElementById('div1').innerHTML='';
}
</script>
    var input = document.createElement('input'); 
input.type = "text"; 
div1.appendChild(input);

堆栈溢出。 LINK 如何在Javascript中动态添加文本框?

您的代码:

 <?php    $sql = mysql_query("SELECT * FROM preturnreason");?>
<th align="left" height="26" colspan="3"><strong>Return Reason Codes:</strong><span class="select">
<select description='returnreason' id='returnreason' >
    <option value="999">--Select Return Reason--</option>';
   <?php
        while($record = mysql_fetch_assoc($sql)) 
               { ?>
                <option value="<?php echo $record['DESCRIPTION'] ?>"> <?php echo $record['DESCRIPTION'] ?> </option>

            <?php  } ?>
    <option value="0">Other</option>';
</select>

     ?> <div id="myTxt"></div></th>
</tr>

下面是我的示例代码,如果用户在下拉菜单中选择“其他”,该示例代码将演示如何显示一个文本框。

的HTML

<select id='sBox' onselect='chkOption()'>
<option value='9'>Select</option>
<option value='0'>Other</option>
<option value='1'>One</option>
</select>

<div id='myTxt' style="display: none">
    <input type='text'>
</div>

jQuery查询

$('select').on('change', function() {

if( this.value  == '0')
  $("#myTxt").show();
else
    $("#myTxt").hide();
})

现场演示

(根据AP的回答)
如果您想使用纯JS(无Jquery),则可以使用以下代码:

<select id='sBox' onChange='checkchange()'>
    <option value='9'>Select</option>
    <option value='0'>Other</option>
    <option value='1'>One</option>
</select>

<div id='myTxt' style="display: none">
    <input type='text'>
</div>

和Javascript:

function checkchange() {
  if(document.getElementById('sBox').value == '0') {
    document.getElementById('myTxt').style.display='block';
  }
  else {
    document.getElementById('myTxt').style.display='none';
  }
};

Jsfiddle: 在这里

结合到您的代码:

<th align="left" height="26" colspan="3"><strong>Return Reason Codes:</strong><span class="select">
<select description='returnreason' id='returnreason' onchange=' checkchange()'>
  <option id="0">   --Select Return Reason--  </option>
  <?php
    $sql = mysql_query("SELECT * FROM preturnreason");
    while($record = mysql_fetch_assoc($sql)) {
      echo '<option value=" '.$record['DESCRIPTION'].'">'.$record['DESCRIPTION']. '</option>';
    }
  ?>
  <option value='other'>Other - please specify</option>
</select>
<div id="div1"><input type='text'></div></th>
</tr>

<script>
function checkchange() {
  if(document.getElementById('returnreason').value == 'other') {
    document.getElementById('div1').style.display='block';
  }
  else {
    document.getElementById('div1').style.display='none';
  }
}
</script>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM