且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在< select>中显示数据基于另一个< select>的值

更新时间:2023-01-07 22:47:28

您需要做的第一件事是将查询结果传递回一个将填充页面中select元素的函数.您的PHP代码似乎未返回任何内容.

First thing you need to do is to pass the result of your query back to a function that will populate the select element in your page. Your PHP code doesn't seem to be returning anything though.

为了轻松地在页面上填充您的元素,请返回一个简单的数组.

In order to easily populate your element on the page, return a simple array.

while(($row =  mysqli_fetch_assoc($rs))) {
        $Myarray[] = $row['Models'];
    }
}

echo json_encode($Myarray);

此外,更改success以运行下面的pop_select函数(或您喜欢的任何名称).

Also, change success to run the pop_select function below (or whatever name you like).

$.ajax({
            url: 'getModels.php',
            type: 'post',
            data: {Manufacturer: Manufacturer},
            //contentType: false,
            success: function(data) {
                    myarray = JSON.parse(data);
                    pop_select(myarray);
            }

这是使用调用结果填充select元素的方式:

And here's how you'd populate the select element with the result of the call:

function pop_select(Myarray) {
var select = document.getElementById("Model");

for(i = 0; i < Myarray.length; i++) {
    var opt = Myarray[i];
    var el = document.createElement("option");
    el.textContent = opt;
    el.value = opt;
    select.appendChild(el);
 }
}