当选择的是阵列时,将一个选择框按另一个选择框填充


Populating One Select Box Based on Another When Selects are Arrays

我知道如何使用jquery和ajax根据来自另一个选择框的选项填充一个选择框,但是当选择框是数组时,您如何做到这一点?

默认情况下,页面上有5个,但是我也使用。enablemultifield()来添加更多的行,因为我需要它们…每周我们都会在库存中增加10到30辆车。

Ajax:

$(".make").change(function() {
var id=$(this).val();
var dataString = 'id='+ id;
$.ajax ({
    type: "POST",
    url: "get_models.php",
    data: dataString,
    cache: false,
    success: function(html) {
        $(".model").html(html);
    } 
});
});
PHP:

            <ul>
            <li><label for="year[]">Year</label><select name="year[]"><option></option>'.$Year.'</select></li>
            <li><label for="year[]">Make</label><select name="make[]" class="make"><option></option>'.$Make.'</select></li>
            <li><label for="year[]">Model</label><select name="model[]" class="model" style="width: 120px"><option></option>'.$Model.'</select></li>
            <li><label for="trim[]">Trim</label><input type="text" size="10" name="trim[]" /></li>
            <li><label for="vin[]">VIN</label><input type="text" name="vin[]"size="20" maxlength="17" onkeyup="return Upper(event,this)" /></li>
            <li><label for="mileage[]">Mileage</label><input type="text" name="mileage[]" size="3" maxlength="6" /></li>
            <li><label for="color[]">Color</label><select name="color[]"><option></option>'.$Color.'</select></li>
            <li><label for="cost[]">Cost</label><input type="text" name="cost[]" size="6" maxlength="6" onchange="currency(this)" /></li>
            <li><label for="asking[]">Asking</label><input type="text" name="asking[]" size="6" maxlength="6" onchange="currency(this)" /></li>
        </ul>

我认为你的问题是,你通过AJAX加载的模型设置在所有的.model。您需要将$('.make').change(...)更改为以下

        $(".make").change(function () {
           var $this = $(this);
           var id = $(this).val();
           var dataString = 'id=' + id;
           $.ajax({
              type: "POST",
              url: "get_models.php",
              data: dataString,
              cache: false,
              success: function (html) {
                 $(".model", $this.closest('ul')).html(html);
              }
           });
        });

这将为.make<ul>中出现的.model设置新的"模型"

以适应动态添加的.make,替换

$(".make").change(function () {

$(document).on('change', 'ul .make', function () {

我对答案的建议是,您的前三个<label>错误地指向year[]而不是year[], make[]model[]。这是在扔掉你的东西吗?它击中了第一个界外球,然后抛了出去。对我来说很有意义…

祝你好运(: