[英]display a textbox if user select “Other” on the drop down menu retrieved from the database
pls help me with this one. 请帮我这个。 I can't display the textbox if the user selects "Other" option in the drop down menu. 如果用户在下拉菜单中选择“其他”选项,则无法显示文本框。 the values in the drop down came from the database. 下拉列表中的值来自数据库。 thanks! 谢谢! :) :)
<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>
and here's my javascript: 这是我的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. 堆栈溢出。 LINK How do I add textboxes dynamically in Javascript? LINK 如何在Javascript中动态添加文本框?
Your Code : 您的代码:
<?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>
Below is my sample code which gives demo to display a text-box if user select “Other” on the drop down menu. 下面是我的示例代码,如果用户在下拉菜单中选择“其他”,该示例代码将演示如何显示一个文本框。
HTML 的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 jQuery查询
$('select').on('change', function() {
if( this.value == '0')
$("#myTxt").show();
else
$("#myTxt").hide();
})
(Answer based on AP's answer) (根据AP的回答)
If you'd rather use pure JS (no Jquery) you can use the following code: 如果您想使用纯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>
and Javascript: 和Javascript:
function checkchange() {
if(document.getElementById('sBox').value == '0') {
document.getElementById('myTxt').style.display='block';
}
else {
document.getElementById('myTxt').style.display='none';
}
};
Combining that to your code: 结合到您的代码:
<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.