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(;
答案 0 :(得分:3)
编辑:感谢@jk - 这将有效:
$('[id^="partner_name_or_description"]').autocomplete(
"__autocomplete_source.php",
//do stuff
});