HTML 输入字段
<input type="text" name="partner_name_or_description[]" id="partner_name_or_description1" class="row_changed1"/>
<input type="text" name="partner_name_or_description[]" id="partner_name_or_description2" class="row_changed2"/>
<input type="text" name="partner_name_or_description[]" id="partner_name_or_description3" class="row_changed3"/>
jquery 自动完成代码的一部分(有效)
$("#partner_name_or_description1:input, #partner_name_or_description2:input, #partner_name_or_description3:input").autocomplete(
"__autocomplete_source.php",
在 jquery 中,有:partner_name_or_description1、2、3 等......而不是这个长长的 1、2、3 等列表。想要使用简短的序列化(或其他可能的方式)。
首先使用此代码获取这些 1,2,3...
$('[id^="partner_name_or_description"]').each(function (index, partner_name_or_description) {
var s_id = partner_name_or_description.id.substring(27);
});
然后,不要尝试制作这样的长列表
$("#partner_name_or_description" + s_id + " :input").serialize().autocomplete(
它不起作用。如果查看源代码,请参阅
$("#partner_name_or_description" + s_id + " :input").serialize().autocomplete(
不明白原因...可能错误地使用了serialize().autocomplete
或者可能不能使用serialize()
而必须使用其他东西。
我无法使用 class="row_changedX"
因为它对于其他目的是必要的(类必须类似于 row_changed1, 2, 3;每行类名称必须不同)。
工作代码
/*Actually do not understand why this is necessary, but if I delete all uncommented, then code does not work*/
function findValue(li) {
/*if( li == null ) return alert("No match!");
// if coming from an AJAX call, let's use the CityId as the value
if( !!li.extra ) var sValue = li.extra[0];
// otherwise, let's just display the value in the text box
else var sValue = li.selectValue;
alert("The value you selected was: " + sValue);*/
}
function selectItem(li) {
findValue(li);
}
function formatItem(row) {
return row[0] + " (id: " + row[1] + ")";
}
$('[id^="partner_name_or_description"]').autocomplete("__autocomplete_source.php",
{
delay:10,
minChars:2,
matchSubset:1,
matchContains:1,
cacheLength:10,
onItemSelect:selectItem,
onFindValue:findValue,
formatItem:formatItem,
autoFill:true
}//{ delay:10,
)//.autocomplete(;
最佳答案
编辑:感谢@jk——这会起作用:
$('[id^="partner_name_or_description"]').autocomplete(
"__autocomplete_source.php",
//do stuff
});
关于javascript - jQuery 使用 id 自动完成多个字段,但不使用 class;如果可能的话使用序列化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16860806/