这是一个有点复杂,所以忍耐一下:AJAX预填充下拉菜单
在form.php的,我有2个下拉菜单:
Plant type
下拉,允许用户选择“植物类型“。
Plant sub-type
下拉是通过AJAX和数据库的调用提出了“植物分型的多元选择。
AJAX代码:
<script type="text/javascript">
function getXMLHTTP() {
var xmlhttp = false;
try {
xmlhttp = new XMLHttpRequest();
}
catch (e) {
try {
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
catch (e) {
try {
xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
}
catch (e1) {
xmlhttp = false;
}
}
}
return xmlhttp;
}
function getPlantSubtype(strURL) {
var req = getXMLHTTP();
if (req) {
req.onreadystatechange = function() {
if (req.readyState == 4) {
if (req.status == 200) {
document.getElementById('plant_subtype_div').innerHTML = req.responseText;
} else {
alert("There was a problem while using XMLHTTP:\n" + req.statusText);
}
}
}
req.open("GET", strURL, true);
req.send(null);
}
}
</script>
完美的作品,当我提出,值将保存在数据库和表格下面的表格所示。
现在用户想要编辑该条目,单击带有info的表格单元格旁边的'Edit'。
值“设备类型”和“植物子类型”通过$_POST
传递给EDIT.php。 (使用Smarty的标签)
EDIT.php代码:
<select name="plant_type" onchange="getPlantSubtype('plant_subtype.php?plant_type='+this.value)">
<option selected="selected" value="{$plant_type}">
{$plant_type}
</option>
<option value="" disabled="disabled">
---
</option>
<option value="1">
Tomato
</option>
<option value="2">
Carrot
</option>
</select>
<div id="plant_subtype_div">
<select name="plant_subtype">
<option selected="selected" value="{$plant_subtype}">
{$plant_subtype}
</option>
<option value="" disabled="disabled">
---
</option>
</select>
</div>
在EDIT.php两个下拉菜单显示正确的值。
但是当我点击Plant sub-type
下拉多选项不可用。
这是因为表单已经预先填入$_POST
而onChange
尚未发生。
如何使Plant sub-type
下拉显示其选择加载EDIT.php什么时候?
混战师 - 不幸的是,没有工作 - 我认为这个问题是您发送的代码仍然挂`onChange` - 我们怎么把它弄出来的存在,并且链接到页面加载? – pepe 2011-02-13 16:33:46