我还没有找到解决这个问题的方法,需要一些新的眼睛来解决这个问题。基本上,您自动完成搜索车辆,并在选择过滤器请求时发送 json 填充模型选择列表。一切正常,除非我创建一行并选择自动完成值,它只链接到第一行模型而不是附加行。我计划根据品牌对模型进行另一个过滤器(不在此示例中)。
JS:
function setAutocomplete() {
$('#vehicle_id').val("");
$text = jQuery(".veh_selector");
$.each($text, function(i, val) {
$(val).autocomplete({
source: 'vehicles.php',
minLength: 2,
select: function(event, ui) {
$('#veh_desc_id').val(ui.item.id);
$('#vehicle_id').val(ui.item.vehicle_id);
$.getJSON("vehicles.php?filter=" + ui.item.vehicle_id, function(jsonData) {
$("select.model").html(""); //clear old options
jsonData = eval(jsonData); //get json array
for (i = 0; i < jsonData.length; i++) //iterate over all options
{
for (key in jsonData[i]) //get key => value
{
$("select.model").get(0).add(new Option(jsonData[i][key], [key]), document.all ? i : null);
}
}
});
}
});
});
}
var current = 0;
var addVehicleFields = function() {
current++;
$newVehicle = $("#searchtemplate").clone();
$newVehicle.removeAttr("id");
$newVehicle.removeClass("hide_element");
$prefix = "extra" + current;
$newVehicle.children("div").children(":input").each(function(i) {
var $currentElem = $(this);
$currentElem.attr("name", $prefix + $currentElem.attr("name"));
});
$newVehicle.appendTo("#vehiclesField");
$('.remove-this').show('fast');
$('#remove-me').removeAttr('disabled');
setAutocomplete();
}
$(document).ready(function() {
setAutocomplete();
$("#addVehicle").live("click", addVehicleFields);
$('.remove-this').live('click', function() {
$(this).parent().remove();
});
});
html
<div id="models-makes">
<fieldset id="vehiclesField">
<label>Search Vehicles</label>
<div class="vehicle"></div>
</div>
</fieldset>
<div>
<div class="model-make original">
<label for="veh_desc"></label>
<input type="hidden" class="veh_selector" id="veh" name="veh" size="30"/>
</p>
<input type="hidden" id="veh_desc_id" name="veh_desc_id" />
<div class="vehicle hide_element" id="searchtemplate">
<input class="veh_selector" id="veh" name="_veh" size="30"/>
<select class="model empty">
<option>Models</option>
</select>
<select class="make empty">
<option>Makes</option>
</select>
<input name="remove-this" class="remove-this" value="Remove" type="button">
</div>
</div>
</div>
<div>
<input type="button" id="addVehicle" value="Add A Vehicle">
<input name="reset" id="reset" value="Reset" type="reset">
</div>