我有一个用户从下拉列表中选择一年的地方 - 他的ID是" newCarYear_1"。当用户选择一年时,它会填充第二个下拉菜单的选项," newCarMake_1"。选择make后,选择" newCarModel_1"人口稠密。它工作正常,除非用户点击"添加汽车"按钮和" newCarYear_2"," newCarMake_2"和" newCarModel_3"如果添加,则不会为品牌和型号填充选项。这是我改变的代码:
$('[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声明。
答案 0 :(得分:2)
试试这个
$(document).on("change",'[id^="newCarYear_"]', 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);
});
});
$(document).on("click","#addNewCar",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");
});