如果用户选择“其他”,则显示一个文本框;在从数据库检索到的下拉菜单上
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);
stackoverflow。链接如何在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();
})
现场演示
<p的回答)>
如果你宁愿使用纯JS(没有Jquery),你可以使用以下代码:
如果你宁愿使用纯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>
相关文章:
- 将输入字段中的文本提交到我的数据库,同时将其添加到我的列表中
- 编辑数据库输入时在文本框上换行
- 使用自动完成文本框从mysql数据库获取表记录并显示在文本字段中
- JavaScript 文本区域值,带有数据库中的新行
- 如何将动态添加的文本框插入MySQL数据库
- onblur文本框生成电子邮件已存在于数据库中的警报
- 如何高亮显示选定的文本并存储在数据库中,以及当页面重新加载时,如何高亮显示回选定的文本
- 单击按钮可多次动态添加选择框、文本框和日期,并将这些值插入数据库
- 剪切从数据库中选择的文本
- 如何显示从MySQL数据库中获取的随机文本和图像
- Php菜单查询数据库并显示文本/链接
- 使用带有数据库信息的下拉菜单填充文本框
- Javascript/PHP,使用文本文件作为数据库,并将内容组织到阵列表中
- 在文本区域的Change上进行AJAX调用以更新数据库
- 如何将翻译后的文本插入数据库 php
- 如何在Web SQL数据库中逐行插入文本文件
- NodeJS:将数据库中检索到的数据以JSON形式转换为纯文本
- 使用数据库中文本框内的复选框自动搜索
- 如何制作本地网站的文本数据库
- 基于多操作系统的文本数据库,带有Python和JavaScript引擎