jQuery 克隆自动完成链仅在第一行链接
我还没有找到解决方案,需要新的几双眼睛来解决这个问题。基本上,您会自动完成搜索车辆,并在选择后发送一个过滤器请求,其中包含填充模型选择列表的 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>
I have not found a solution to this and a new few set of eyes is needed to solve this issue. Basically you autocomplete search the vehicle and upon select a filter request is sent with json filling the models select list. Everything works fine except when I create a row and select the autocomplete value it only chains to the first row models not the appended row. I plan on doing another filter for the models based on makes (not in this example).
The 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();
});
});
The 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>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我只在测试服务器上没有任何地方,因为真的没有什么可放的。下面是车辆的 sql ex 和两个文件:getvehicles.php 和 makevehicles.php。还有一个更新的js。感谢
SQL
getvehicles.php
makevehicles.php
更新了 JS。我所做的就是更改上面两个文件的自动完成和 getJSON url 中的源。我的脚本非常复杂,所以就把这两个去掉了。让我知道这是否有帮助。顺便说一句:我修复了 html,但仍然不行。这里使用jqueryui。如果您也需要,请告诉我。
I don't have it up anywhere only on test server, as really nothing to put up. Below is a sql ex for the vehicles and two files: getvehicles.php and makevehicles.php. Also is an updated js. Thanks
SQL
getvehicles.php
makevehicles.php
Updated JS. All I did was change source in autocomplete and getJSON url for two files above. My script is very complex so just took these two out. Let me know if this helps. BTW: I fixed the html and still no go. This uses jqueryui. If you need that too let me know.