検索対象の各変数に3つの属性(ラベル、ID、値)があるオートコンプリートを使用しています。ユーザー選択の値を、その選択のIDに基づいて4つの非表示フィールドの1つに渡す必要があります。
jsファイル
var destinations= [
{value: "25",label: "USA",id: "1"},
{value: "26",label: "Midwest",id: "2"},
{value: "27",label: "Colorado",id: "3"},
{value: "28",label: "Denver",id: "4"}
];
$(document).ready(function() {
$( "#destinations" ).autocomplete({
minLength: 1,
source: destinations,
select: function( event, ui ) {
$('#destinations').val( ui.item.label );
$('#destinationid').val(ui.item.id);
$('#destinationtype').val(ui.item.value);
if ($("#destinationid").val() == 1){
$("#results1").text($('#type1').val());
}
if ($("#destinationid").val() == 2){
$("#results2").text($('#type2').val());
}
if ($("#destinationid").val() == 3){
$("#results3").text($('#type3').val());
}
if ($("#destinationid").val() == 4){
$("#results4").text($('#type4').val());
}
},
focus: function( event, ui ) {
$("#destinations").val( ui.item.label );
return false;
}
});
});
私のhtmlには、次のものがあります:(テスト目的で非表示にしません)。
<input id="destinations" />
<input type="hidden" input id="type1" />
<input type="hidden" input id="type2" />
<input type="hidden" input id="type3" />
<input type="hidden" input id="type4" />
自動選択はラベルをうまく引っ張っていますが、私が1つを選択すると、ラベルを保持するのではなく、選択をその値に変更します。また、対応するテキストボックスに値を入力せず、最初のテキストボックスに値を残すだけです。
任意の提案をいただければ幸いです。