[英]Why is my dynamic dropdown list not working?
假定下拉列表是從另一個表的列中獲取記錄,但是當前沒有記錄出現。 另外,如果用戶在列表中找不到想要的內容,我將需要在下拉列表中顯示一個“其他”選項,以便用戶鍵入。 這是我的代碼:
<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>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.