我知道如何使用 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);
}
});
});
这只会为 .model
设置新的“模型”目前已更改 .make
的<ul>
更新
适应动态添加的.make
,替换
$(".make").change(function () {
与
$(document).on('change', 'ul .make', function () {
关于php - 当选择是数组时,根据另一个选择框填充一个选择框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15755418/