I'm trying to do something开发者_如何学JAVA like so:
$("[type=text]").autocomplete({
source: "./json.php?id="+$(this).attr("id"),
minLength: 2
}).addClass("ui-widget ui-widget-content ui-corner-left");
Obviously the source: "./json.php?id="+$(this).attr("id")
doesn't work. Does anyone know how I can do this? There will be many autocomplete fields so I can't use a selector like $('#ID')
.
If you need to set this for many elements at the same time, use an each()
loop:
$("[type=text]").each(function() {
var thisEl = $(this);
thisEl.autocomplete({
source: "./json.php?id=" + thisEl.attr("id"),
minLength: 2
});
// whatever else you want to do with the element
});
You have to iterate through them using .each()
to execute .attr()
in proper scope:
$("input[type=text]").each(function(){
$(this).autocomplete({
source: "./json.php?id="+$(this).attr("id"),
minLength: 2
}).addClass("ui-widget ui-widget-content ui-corner-left");
}
Edit: As mentioned in a comment, you have a LOT of fields, like 1500+. You might try improve performance by adding a class to each field, and using the .classname
selector, or at least narrow down the initial search to input[type="text"]
, although jQuery might do that already for optimization.
You might also try setting the source option in the create
or search
events of the autocomplete, although the latter might not work. Doesn't hurt to see how it performs:
$("input[type=text]").autocomplete({
search: function(){
$(this).autocomplete("option", "source", "./json.php?id="+$(this).attr("id"));
},
minLength: 2
}).addClass("ui-widget ui-widget-content ui-corner-left");
Alternatively you can even bind into the focus event of the input field itself to set the source:
$("input[type=text]").autocomplete({
minLength: 2
}).focus(function(){
$(this).autocomplete("option", "source", "./json.php?id="+$(this).attr("id"));
}).addClass("ui-widget ui-widget-content ui-corner-left");
See if any of those improve performance, it's an interesting test case you've got there.
精彩评论