簡體   English   中英

如何向 jQuery 小部件“自動完成”添加第二個變量值?

[英]How do I add a second variable value to the jQuery widget 'Autocomplete'?

我已經使用 jQuery 'Autocomplete Widget' 很多年了。 這一直是這樣做的,但是像這樣將帶有“term”的值傳遞給 PHP SQL 代碼;

$( "#cs1" ).autocomplete({
        autoFocus: true,
        minLength: 3,
        source: "gethint.php",

        select: function( event, ui ) {
                // This if undes the readonly on the Fname input field below
                if ( ui.item.label == 'NONHAM' ) {$('#Fname').prop('readonly', false);}
                  $( "#cs1" ).val( ui.item.label ); 
                  $( "#hints" ).val( ui.item.value );
                  $( "#Fname" ).val( ui.item.desc );
                  var nc = $( "#thenetcallsign" ).html();
                     //return false;
                 }
    })
    
    .data( "ui-autocomplete" )._renderItem = function( ul, item ) {
               return $( "<li>" )
               .append( "<a>" + item.label + " --->  " + item.desc + "</a>" )
               .appendTo( ul );
            };
});

但是現在我必須在 SQL 代碼中添加另一個條件以返回更詳細的值。 此附加條件的值是;

var nc = $( "#thenetcallsign" ).html();

問題是我不知道如何將它添加到“term”或單獨的變量中,並使用“自動完成”小部件將其傳遞給 gethint.php。

一旦我獲得了 PHP 程序的額外價值,我就知道該怎么做。

有人可以解釋或告訴我如何做到這一點嗎?

您可能需要對其進行字符串化但傳遞extraParams

extraParams: { type: "CoolCode" },

 var nc = $("#thenetcallsign").html(); $("#cs1").autocomplete({ autoFocus: true, minLength: 3, source: "gethint.php", extraParams: { nc: nc }, select: function(event, ui) { // This if undes the readonly on the Fname input field below if (ui.item.label == 'NONHAM') { $('#Fname').prop('readonly', false); } $("#cs1").val(ui.item.label); $("#hints").val(ui.item.value); $("#Fname").val(ui.item.desc); var nc = $("#thenetcallsign").html(); //return false; } }) .data("ui-autocomplete")._renderItem = function(ul, item) { return $("<li>") .append("<a>" + item.label + " ---> " + item.desc + "</a>") .appendTo(ul); }; });

source可以是一個函數,以獲得更大的靈活性

$( "#cs1" ).autocomplete({
 /* ... */
        source: function(request, response) {
              $.getJSON( "gethint.php", {
                   term: request.term ,
                   nc:  $( "#thenetcallsign" ).html()
              }, response );
        },
 /* ... */
});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM