English 中文(简体)
AJAX预先填充的下拉菜单
原标题:AJAX pre-populated drop down menu

这有点复杂,所以请耐心等待:

在FORM.php上,我有两个下拉菜单:

设备类型下拉菜单,允许用户选择设备类型。

Plant子类型下拉菜单,通过AJAX和对数据库的调用为Plant子类型提供多种选择。

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:
" + req.statusText);
                    }
                }
            }
            req.open("GET", strURL, true);
            req.send(null);
        }
    }
</script>

这非常有效,当我提交时,值会保存在DB中,并显示在表单下面的表中。

现在用户想要编辑这个条目,点击带有信息的表格单元格旁边的编辑。

设备类型和设备子类型的值通过$_POST传递到EDIT.php。

EDIT.php代码(使用Smarty标记):

<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子类型下拉菜单时,多项选择不可用。

这是因为表单已经预先填充了$_POST,而onChange尚未发生。

加载EDIT.php时,如何使Plant子类型下拉菜单显示其选择?

最佳回答

一种选择是在页面加载时调用getPlantSubtype.php?plant_type={$plant_type})

例如(在页面底部):

<script type="text/javascript">
    getPlantSubtype( plant_subtype.php?plant_type={$plant_type} )
</script>

更好的选择是从同一个地方提取子类型plant_subtype.php从(cache,db)中提取子类型并填充选项值。

问题回答

暂无回答




相关问题
ajax login using httpRequest?

I am trying to develop my login script to give feedback to the user if the login is valid or not. Basically if it isn t correct a div box will show saying its wrong, if its correct it will show its ...

Virtual Tour using sketch up, ajax, flash technologies

I want to know if there are existing technology that make your 3d models in sketch into virtual tours, using either Ajax or Flash for web presentation. If there s none, which will be a good approach ...

How can i update div continuously

I have asp.net application where i have a div which showing the value from other site. The value of that site is changing continuously. I want that my div will automatically update in some interval ...

热门标签