请帮我一下。如果用户在下拉菜单中选择"其他"选项,则无法显示文本框。下拉列表中的值来自数据库。谢谢!:)
<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>