2012-01-08 51 views
0

我想就與屬性「kolonadi」Jquery自動完成後按下第二個鍵運行?

當我按在文本框的主要使用每個文本框自動完成工作,頁面提醒我「的keydown enterance」,但自動完成未運行。如果我再按一個鍵,它就可以正常工作。

我該如何修改此代碼?

這是我的動態輸入:

<input name="ctl00$MainContent$qtxt_UNVAN" type="text" id="MainContent_qtxt_UNVAN" class="textEntry2 ui-autocomplete-input" kolonadi="UNVAN" style="width:200px;" autocomplete="off" role="textbox" aria-autocomplete="list" aria-haspopup="true"> 

這是jQuery的自動完成:

$('.textEntry2').keydown(function() { 
    alert("keydown enterance"); 
    var kolonadi_ = $(this).attr("kolonadi"); 

    $(this).autocomplete({ 
     source: function (request, response) { 
      $.ajax({ 
       url: '<%=ResolveUrl("~/AutoCom.asmx/GetValues") %>', 
       data: "{ 'word': '" + request.term + "','KullaniciIndexInGlob':'<%=KullaniciIndexInGlob %>','BaslikId':'<% =BaslikId %>','columnName':'" + kolonadi_ + "'}", 
       dataType: "json", 
       type: "POST", 
       contentType: "application/json; charset=utf-8", 
       success: function (data) { 
        response($.map(data.d, function (item) { 
         return { 
          label: item.split('-')[0], 
          val: item.split('-')[1] 
         } 
        })) 
       }, 
       error: function (response) { 
        alert(response.responseText); 
       }, 
       failure: function (response) { 
        alert(response.responseText); 
       } 
      }); 
     }, 
     select: function (e, i) { 
      $("#<%=hfCustomerId.ClientID %>").val(i.item.val); 
     }, 
     minLength: 1 
    }); 
}); 
+0

這個插件的文檔是[這裏](http://www.jqueryui.com/demos/autocomplete)。他們都沒有建議在keydown上應用這個widget。我會訪問該頁面並查看示例。 – 2012-01-08 22:25:38

回答

2

我要讓每個autocomplate使用文本框的文本框ATTR「kolonadi」

然後,你需要做的是a jQuery selector該屬性相匹配:

$('input[type="text"][kolonadi!=""]').each(function() { 
    // ... 
}); 

...當我按下文本框中的一個鍵,頁面提醒我「keydown enterance」,但autocomp沒有運行。如果我再按一個鍵,它就會運行!

問題是,the jQuery UI .autocomplete method不會立即降低下拉,就像您認爲的那樣。如果您只調用一次,它會將輸入字段永久轉換爲自動完成字段。

因此,您的代碼所做的是檢查按鍵,如果發現它,它會將文本字段變成自動完成。然後當您第二次輸入按鍵時,自動完成處理程序運行您的處理程序運行,並且它會再次轉換爲自動完成

所以只需在頁面加載時直接調用.autocomplete,擺脫​​處理程序,並將其稱爲完成。您不需要您自己的按鍵處理程序,因爲.autocomplete方法將插入其自己的按鍵處理程序。

事情是這樣的:

var textEntry2 = $('.textEntry2'); 
var kolonadi_ = textEntry2.attr("kolonadi"); 

textEntry2.autocomplete({ 
    source: function(request, response) { 
     $.ajax({ 
      url: '<%=ResolveUrl("~/AutoCom.asmx/GetValues") %>', 
      data: "{ 'word': '" + request.term + "','KullaniciIndexInGlob':'<%=KullaniciIndexInGlob %>','BaslikId':'<% =BaslikId %>','columnName':'" + kolonadi_ + "'}", 
      dataType: "json", 
      type: "POST", 
      contentType: "application/json; charset=utf-8", 
      success: function(data) { 
       response($.map(data.d, function(item) { 
        return { 
         label: item.split('-')[0], 
         val: item.split('-')[1] 
        } 
       })) 
      }, 
      error: function(response) { 
       alert(response.responseText); 
      }, 
      failure: function(response) { 
       alert(response.responseText); 
      } 
     }); 
    }, 
    select: function(e, i) { 
     $("#<%=hfCustomerId.ClientID %>").val(i.item.val); 
    }, 
    minLength: 1 
}); 
2

編輯:前面的代碼不工作,這是必要的,從類的所有輸入迭代textEntry2

您必須在$(document).ready函數中調用自動完成功能,而不是在每個keydown中。假設您要在其中使用自動完成所有的輸入都來自textEntry2類,你需要這樣的:

<script type="text/javascript"> 
$(document).ready(function(){ 
    $('input.textEntry2').each(function() { 
     var kolonadi_ = $(this).attr("kolonadi"); 
     $(this).autocomplete(/* do all stuff here */); 
    }); 
}); 
</script> 
+0

這隻回答了一半的問題,或者我已經給出了upvote。另一半這個答案錯過了'我想在每個文本框上使用屬性「kolonadi」來自動完成工作' – 2012-01-08 22:28:31

+0

@merlyn yes,請參閱編輯。 – 2012-01-08 22:38:55

相關問題