2012-07-13 32 views
0

我有一個Web應用程序,其中我使用頁面加載時使用來自數據庫的項目填充multiselectlistbox。在jquery中使用.keypress事件檢測輸入按鈕

我從列表框中獲取所有選中的項目,但它們都是按順序填充的。

我希望這些項目按它們被用戶選擇的順序排列。

因此對於這個問題,我發現解決方案使用客戶端.click()事件在列表框中,因爲我檢測選定的項目,並保持它的值在hiddenfield.This運作良好,但問題出現時,用戶按下輸入鍵點擊鼠標點擊選擇項目。這時它不火。點擊()事件,所以我已經使用 onkeypress()onkeyup()但它在所有

這裏犯規火是我的代碼:

<script type="text/javascript"> 
    $(document).ready(function() { 
     $(".chzn-results .active-result").click(function(event) {         
      var hdfld = document.getElementById("hdn_menuitems"); 
      if (hdfld.value == "") { 
       hdfld.value = $(this).index(); 
      } 
      else { 
       hdfld.value = hdfld.value + "," + $(this).index(); 
      }        
     }); 
     $(".chzn-results .active-result").keydown(function(event) { 
      $(".chzn-results .active-result").keyup(function(event) {        
       alert("Event"); 
       if(event.keyCode==13) 
       {   
        var hdfld = document.getElementById("hdn_menuitems"); 
        if (hdfld.value == "") { 
         hdfld.value = $(this).index(); 
        } 
        else { 
         hdfld.value = hdfld.value + "," + $(this).index(); 
        }       
       } 
      }); 
     });  
</script> 

這是我的列表框:

<asp:ListBox ID="dr_menuitem" runat="server" class="chzn-select" style="width:575px;" SelectionMode="Multiple" > 
    <asp:ListItem></asp:ListItem> 
</asp:Listbox> 

我一直在使用使用列表框[http://harvesthq.github.com/chosen/][1]

回答

0

做到這一點的方法: -

$('.chzn-results .active-result').keydown(function(e) { 
    if (e.keyCode == 13) 
    { 
     var hdfld = $('#hdn_menuitems'); 
     if (hdfld.value == "") 
      $(hdfld).val($(this).index()); 
     else 
      $(hdfld).val(hdfld.value + "," + $(this).index()); 
    } 
});​ 
+0

Siva Charan,它不起作用 這個事件不會在按下輸入事件時被解僱。 – sharad 2012-07-14 05:49:49