我有一个用户从下拉列表中选择年份的位置 - 谁的 id 是“newCarYear_1”。当用户选择一年时,它会填充第二个下拉列表“newCarMake_1”的选项。选择品牌后,将填充“newCarModel_1”的选择选项。它工作正常,除了当用户点击“添加汽车”按钮并添加“newCarYear_2”、“newCarMake_2”和“newCarModel_3”时,不会为品牌和型号填充选择。这是我的 on 更改代码:
$('[id^="newCarYear_"]').on("change", function() {
var thisid = "#" + $(this).attr('id');
var year = $(thisid).val();
$(thisid.replace("Year", "Make") + " option").remove();
$.get("./ajax/getCarMakes.php", { year: year }, function(data) {
$(thisid.replace("Year", "Make")).append(data);
});
});
这是用户点击“添加汽车”按钮时的代码:
$("#addNewCar").click(function() {
newCarNum++; // declared elsewhere in my js script - set to 1 when page loads
var globalLoanLength = $("#globalLoanLength").val();
var globalAPR = $("#globalAPR").val();
var insertString = "<tr"; if (newCarNum % 2 == 0) insertString = insertString + " style='background-color:#f8f8f8'"; insertString = insertString + "><td><span style='line-height:34px;'>Year:</span> <select id='newCarYear_" + newCarNum + "' name='newCarYear_" + newCarNum + "' style='position:relative; top: 5px; width:89px;'><option value=''>-- Year --</option><?php for ($j = 0; $j < count($carYears); $j++) echo "<option value='" . $carYears[$j]['year'] . "'>" . $carYears[$j]['year'] . "</option>"; ?></select> <span style='line-height:34px;'>Make:</span> <select id='newCarMake_" + newCarNum + "' name='newCarMake_" + newCarNum + "' style='position:relative; top: 5px; width:160px;'><option value=''>-- Make --</option></select> <span style='line-height:34px;'>Model:</span> <select id='newCarModel_" + newCarNum + "' name='newCarModel_" + newCarNum + "' style='position:relative; top: 5px; width:160px;'><option value=''>-- Select A Make--</option></select> <span style='line-height:34px;'>Trim:</span> <select id='newCarTrim_" + newCarNum + "' name='newCarTrim_" + newCarNum + "' style='position:relative; top: 5px; width:160px;'><option value=''>-- Select A Model --</option></select> <span style='line-height:34px;'>Sale Price:</span> <input id='newCarSalePrice_" + newCarNum + "' name='newCarSalePrice_" + newCarNum + "' type='text' placeholder='-- Sale Price --' style='position:relative; top:5px; width:80px;' onkeyup=\"this.value = this.value.replace(/[^.0-9]/gi,'');\" /> <span style='line-height:34px;'>VIN:</span> <input id='newCarVIN_" + newCarNum + "' name='newCarVIN_" + newCarNum + "' type='text' placeholder='-- VIN --' style='position:relative; top:5px; width:85px;' /> <span style='line-height:34px;'>Stock Number:</span> <input id='newCarStockNumber_" + newCarNum + "' name='newCarStockNumber_" + newCarNum + "' type='text' placeholder='-- Stock Num --' style='position:relative; top:5px; width:80px;' /> <span style='line-height:34px;'>Loan Length:</span> <input id='newCarLoanLength_" + newCarNum + "' name='newCarLoanLength_" + newCarNum + "' type='text' value='" + globalLoanLength + "' placeholder='Loan Length' style='position:relative; top: 5px; width:50px;' /> <span style='line-height:34px;'>APR:</span> <input id='newCarAPR_" + newCarNum + "' name='newCarAPR_" + newCarNum + "' type='text' value='" + globalAPR + "' placeholder='-- APR --' style='position:relative; top: 5px; width:50px;' /></td></tr>";
$("#newCarTable tr").eq(-2).before(insertString);
if (newCarNum % 2 == 0)
$("#newCarsSubmitRow").css("background-color", "#ffffff");
else
$("#newCarsSubmitRow").css("background-color", "#f8f8f8");
});
我不明白为什么添加的汽车没有触发 .on 声明。