2012-03-13 11 views
0

jQueryUIで別のフィールド(Idのような)を取得するにはどうすればいいですか? (MVCカミソリ)jQueryUIオートコンプリートのid値

私はautocompleteを使用していますが、動作しますが、ラベルに加えてフィールド(または複数のフィールド)が必要です。あなたのマークアップで

+0

はあなたには、いくつかのより多くの詳細を提供することができますか?ユーザーが値を選択したときに 'input'に' id'を入れますか? –

+0

私は入力があります。 'autocomplete'を使うと、検索結果に' BookTitle'のようなラベルを付けることができますが、 'BookId'のように別のフィールドを結果として得ることができます。 – MSajjadi

答えて

0

:あなたのjsの中

<input type="text" class="item" id="input_autocomplete_1" custom_attr="XXX" /> 

 $('.item').autocomplete({ 
        source: function(request, response) { 
//to send the custom attribute to the populate function 
         customAttr = this.element.attr('custom_attr'); 
         $.ajax({ 
          url: "", 
          dataType: "json", 
          data: { 
           term : request.term, 
           custom_attr: customAttr 
          }, 
          success: function(data) { 
//return a json with label, id and custom 
           response($.map(data, function(item) { 
            return { 
             label: item.label, 
             id: item.id, 
             custom: item.custom 
            } 
           })); 
          } 
         }); 
        }, 
        select: function(event, ui) { 
//if you want to do something when the item is selected 
         //you can access: 
         //ui.item.id; 
         //ui.item.custom; 
        } 
       }); 
関連する問題