【发布时间】:2017-03-16 04:02:11
【问题描述】:
我是 jQuery 新手,遇到一个问题,我无法发布源自依赖动态下拉列表的值。
这是我写的代码:
index.php
<form name="form1" action="test.php" method="post">
<table>
<tr>
<td>
<select id="branddd" onchange="change_brand()" required>
<option disabled="disabled" selected="selected" style="color:gray" required >brand</option>
<?php
$res=mysqli_query($link,"select * from brand");
while($row=mysqli_fetch_array($res))
{
?>
<option name="brand" value="<?php echo $row["id"];?>"><?php echo $row["name"];?></option>
<?php
}
?>
</select>
</td>
</tr>
<tr>
<td>
<div id="model">
<select name="model">
<option required>model</option>
</select>
</td>
</tr>
<tr>
<td><input type="submit" value="submit"></td>
</tr>
</table>
这是我编写的将数据从 MySQL 加载到第二个下拉框的 JavaScript:
<script type="text/javascript">
function change_brand()
{
var xmlhttp=new XMLHttpRequest () ;
xmlhttp.open("GET","ajax.php? brand="+document.getElementById("branddd").value,false) ;
xmlhttp.send(null) ;
document.getElementById("model").innerHTML=xmlhttp.responseText ;
}
</script>
ajax.php:
<?php
$link=mysqli_connect("localhost","root","");
mysqli_select_db($link,"test_db");
$brand=$_GET["brand"];
if($brand!="")
{
$res=mysqli_query($link,"select * from model where brand_id=$brand");
echo "<select>";
while($row=mysqli_fetch_array($res))
{
echo "<option>"; echo $row["name"]; echo "</option>";
}
echo "</select>";
}
?>
【问题讨论】:
-
您说您正在尝试发布但您正在使用 GET。要使用 jquery 发布,您可以使用 $.post
-
你必须有你的第一个选择的名字
-
谢谢我忘了我只是添加它,但我得到 id 值然后是 name 值