为什么我的动态下拉列表无效?

时间:2016-01-17 11:50:06

标签: javascript php mysql dynamic dropdown

下拉列表假设从另一个表的列中获取记录,但目前没有出现记录。此外,我需要在下拉列表中选择一个选项,其中包括"其他"如果用户无法在列表中找到他们想要的内容,则可以键入。 这是我的代码:

<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>

1 个答案:

答案 0 :(得分:0)

将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>