【问题标题】:detect enter button pressed using .keypress event in jquery在 jquery 中使用 .keypress 事件检测按下的输入按钮
【发布时间】:2012-07-13 14:49:34
【问题描述】:

我有一个 Web 应用程序,我在其中使用页面加载时数据库中的项目填充多选列表框。

我从列表框中获取所有被选中的项目,但它们是按照它们填充的顺序排列的。

我希望这些项目按照用户选择的顺序排列。

所以对于这个问题,我在列表框上使用客户端.click() 事件找到了解决方案,因为我检测到选择的项目并将其值保留在隐藏字段中。这也很好,但是当用户按下回车键插入鼠标单击时,问题就出现了选择项目。此时它不会触发 .click() 事件,所以我使用了 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] 使用列表框

【问题讨论】:

    标签: jquery asp.net listbox keypress multi-select


    【解决方案1】:

    这样做:-

    $('.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());
        }
    });​
    

    【讨论】:

    • Siva Charan,它不起作用此事件不会在按 Enter 事件时触发。
    猜你喜欢
    • 2013-09-23
    • 1970-01-01
    • 1970-01-01
    • 2016-10-31
    • 1970-01-01
    • 1970-01-01
    • 2012-10-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多