gpt4 book ai didi

javascript - 为什么我的动态下拉列表不起作用?

转载 作者:行者123 更新时间:2023-11-29 21:37:11 26 4
gpt4 key购买 nike

下拉列表应该从另一个表的列中获取记录,但目前没有显示任何记录。另外,我需要在下拉列表中显示“其他”选项,以便用户在无法在列表中找到所需内容时键入。这是我的代码:

<script type="text/javascript">
function showfield(name){
if(name=='Other')document.getElementById('div1').innerHTML='Please Specify: <input type="text" name="other" />';
else document.getElementById('div1').innerHTML='';
}
</script>
<label for="issue_type">Issue Type</label>
<?php
include ("../db/dbConn.php");
$sql = "SELECT issue_type FROM issue where deleted =0";
$result=mysql_query($sql);
echo '<select class ="form-control" type="text" name="issue_type" id="issue_type" onchange="showfield(this.options[this.selectedIndex].value)" >';
while ($row = mysql_fetch_array($result))
{
echo "<option value='".$row['issue_type']."'>".$row['issue_type']." </option>";
}
echo "</select>";

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

最佳答案

将 html 部分分配给一个变量,然后回显它。尝试一下

<script type="text/javascript">
function showfield(name){
if(name=='Other')document.getElementById('div1').innerHTML='Please Specify: <input type="text" name="other" />';
else document.getElementById('div1').innerHTML='';
}
</script>
<label for="issue_type">Issue Type</label>
<?php
include ("../db/dbConn.php");
$sql = "SELECT issue_type FROM issue where deleted =0";
$result=mysql_query($sql);

$htm = '';
$htm .='<select class ="form-control" type="text" name="issue_type" id="issue_type" onchange="showfield(this.options[this.selectedIndex].value)" >';
while ($row = mysql_fetch_array($result))
{
$htm .="<option value='".$row['issue_type']."'>".$row['issue_type']." </option>";
}
$htm .= "<option value='Other'>Other</option>"; // add other option
$htm .="</select>";
echo $htm;
?>
<div id="div1"></div>

关于javascript - 为什么我的动态下拉列表不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34837983/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com