如何将额外参数传递给jQuery AutoComplete字段?
-
02-10-2019 - |
题
我正在使用一种形式的jQuery自动完成。
基本表单从我的数据库中选择产品。这很好,但是我想进一步开发,以便只返回从某个拉链码中运送的产品。我已经弄清楚了后端脚本。我只需要解决将Zipode传递到此脚本的最佳方法。
这就是我的形态。
<form>
<select id="zipcode">
<option value="2000">2000</option>
<option value="3000">3000</option>
<option value="4000">4000</option>
</select>
<input type="text" id="product"/>
<input type="submit"/>
</form>
这是jQuery代码:
$("#product").autocomplete
({
source:"product_auto_complete.php?postcode=" + $('#zipcode').val() +"&",
minLength: 2,
select: function(event, ui){
//action
}
});
该代码在一定程度上起作用。但是,仅返回第一个ZipCode值,无论实际选择哪个值。我想发生了什么事是源URL已在页面加载上进行底漆,而不是在更改选择菜单时。有没有解决的办法?还是总体上有更好的方法可以实现我追求的结果?
解决方案
您需要使用其他方法 source
呼叫,这样:
$("#product").autocomplete({
source: function(request, response) {
$.getJSON("product_auto_complete.php", { postcode: $('#zipcode').val() },
response);
},
minLength: 2,
select: function(event, ui){
//action
}
});
这种格式使您可以通过任何值 当运行时, ,而不是 当它被绑定时.
其他提示
这不是复杂的男人:
$(document).ready(function() {
src = 'http://domain.com/index.php';
// Load the cities straight from the server, passing the country as an extra param
$("#city_id").autocomplete({
source: function(request, response) {
$.ajax({
url: src,
dataType: "json",
data: {
term : request.term,
country_id : $("#country_id").val()
},
success: function(data) {
response(data);
}
});
},
min_length: 3,
delay: 300
});
});
我相信你是正确的呼吁 $("#product").autocomplete
在页面加载上发射。也许您可以将OnChange()处理程序分配给选择菜单:
$("#zipcode").change(resetAutocomplete);
并使其无效 #product
autoComplete()调用并创建一个新的。
function resetAutocomplete() {
$("#product").autocomplete("destroy");
$("#product").autocomplete({
source:"product_auto_complete.php?postcode=" + $('#zipcode').val(),
minLength: 2,
select: function(event, ui){... }
});
}
您可能希望您的resetautocomplete()呼叫变得更聪明 - 例如检查邮政编码是否实际上与最后一个值有所不同 - 保存一些服务器调用。
jQuery("#whatJob").autocomplete(ajaxURL,{
width: 260,
matchContains: true,
selectFirst: false,
minChars: 2,
extraParams: { //to pass extra parameter in ajax file.
"auto_dealer": "yes",
},
});
这项工作给我。覆盖事件 search
:
jQuery('#Distribuidor_provincia_nombre').autocomplete({
'minLength':0,
'search':function(event,ui){
var newUrl="/conf/general/provincias?pais="+$("#Distribuidor_pais_id").val();
$(this).autocomplete("option","source",newUrl)
},
'source':[]
});
$('#product').setOptions({
extraParams: {
extra_parameter_name_to_send: function(){
return $("#source_of_extra_parameter_name").val();
}
}
})
$('#txtCropname').autocomplete('Handler/CropSearch.ashx', {
extraParams: {
test: 'new'
}
});
希望这将帮助某人:
$("#txt_venuename").autocomplete({
source: function(request, response) {
$.getJSON('<?php echo base_url(); ?>admin/venue/venues_autocomplete',
{
user_id: <?php echo $user_param_id; ?>,
term: request.term
},
response);
},
minLength: 3,
select: function (a, b) {
var selected_venue_id = b.item.v_id;
var selected_venue_name = b.item.label;
$("#h_venueid").val(selected_venue_id);
console.log(selected_venue_id);
}
});
默认的“项”将被新的参数列表替换,因此您需要再次添加。
不隶属于 StackOverflow