【问题标题】:hotkeys with jquery带有 jquery 的热键
【发布时间】:2012-06-02 06:22:36
【问题描述】:

我正在使用 jquery 在 Alt+S 上保存一个表单,并且我还在检查必填字段。它工作正常,但我有一个问题。当我在输入必填字段之前按 Alt+S 时,它会给我必填字段的错误,而当我在必填字段中输入任何内容时,表单就会被保存。我希望在填写字段时出错并且如果用户再次按 Alt+S 则表单应该被保存。

jQuery 代码

$(document).ready(function () {
    var isAltKey = false;
    var isShiftKey = false;
    document.onkeyup = function (e) {
        if (e.which == 18) isAltKey = false;
        if (e.which == 16) isShiftKey = false;
    }

    document.onkeydown = function (e) {
        if (e.which == 18) isAltKey = true;
        if (e.which == 16) isShiftKey = true;

        //ALT+S
        if (e.which == 83 && isAltKey == true) {
            TriggerSaveButton();
            StopDefaultAction(e);
        }

        else if (e.which == 9 && isShiftKey == true) {
            if (document.getElementById("cmbUnder_OptionList").focus() == true) {
                document.getElementById("txtGroupSname").focus();
                StopDefaultAction(e);
            }
        }

    }
    function StopDefaultAction(e) {
        if (e.preventDefault) { e.preventDefault() }
        else { e.stop() };
        e.returnValue = false;
        e.stopPropagation();
    }



    function TriggerSaveButton() {
        var groupname = $('#txtGroupName').val();
        if ($('#tab2').is(":visible")) {
            if ((document.getElementById('<%= txtGroupName.ClientID %>').value == "") && ($('<%= listboxdestination.ClientID %>').children().length == 0)) {
                ValidatorEnable(document.getElementById('<%= reqtxtGroupName.ClientID %>'), true);
                ValidatorEnable(document.getElementById('<%= reqlstboxdest.ClientID %>'), true);
                return false;
            }

            else if (groupname != "" && ($("#<%= listboxdestination.ClientID %> option").length != 0)) {
                javascript: __doPostBack('<%=btnaddfrmSave.UniqueID %>', '');

            }
        }
        else {
            if ((document.getElementById('<%= txtGroupName.ClientID %>').value == "")) {
                ValidatorEnable(document.getElementById('<%= reqtxtGroupName.ClientID %>'), true);
                return false;
            }

            else if (groupname != "") {
                javascript: __doPostBack('<%=btnaddfrmSave.UniqueID %>', '');

            }

        }

    }
});    

【问题讨论】:

  • 你能把你的表格的更多代码发给我吗?所以会很容易发现问题..?

标签: jquery asp.net c#-4.0


【解决方案1】:

在我看来,你应该使用 Jquery 表单验证而不是 Asp.net 的 Requirefield 验证器。

目前,您在 from 中使用必填字段验证器可以将其替换为 Jquery 客户端验证。 所以它可能会解决你的问题。

请参阅如何验证使用 Jquery。

  1. jquery for validation Plugins
  2. formValidator

【讨论】:

  • rest 是 asp.net 表单,使用必填字段验证器很简单
  • 嗨,@asifa 请显示您的解决方案的参考链接。我在我的答案中添加了两个参考链接..
  • 我检查了 jquery 验证,但我只需要使用 asp.net 验证器。请您使用与上述相同的代码提供帮助。我的 asp.net 表单是 pastie.org/4012516
  • 调查一下。对于表单验证示例:pastie.org/4012638 我为“txtNotes”控件添加了必填字段验证,我为验证文本控件(“txtNotes”)添加了一个“必需”类..
  • 您需要添加脚本文件来验证表单元素.. 1 用于 Jquery 框架和其他用于验证插件.. 到您的表单
【解决方案2】:

代码:

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
        <script>$(function(){
        $(document).keyup(function(event){
                if(event.which == 65){
                    alert("The Letter A was pressed");
                }
        });
    });
    </script>

每个键都有一个数值,例如,a 等于 65,b 等于 66(以此类推)。

http://youtu.be/xXZsTNwfMNc 这是一个教程,将向您详细展示如何在 jQuery 中使用热键。

【讨论】:

  • 请注意,link-only answers are discouraged,SO 答案应该是搜索解决方案的终点(相对于另一个参考中途停留,随着时间的推移往往会变得陈旧)。请考虑在此处添加独立的概要,并保留链接作为参考。
猜你喜欢
  • 1970-01-01
  • 2010-12-21
  • 2011-12-13
  • 2015-04-17
  • 2019-05-26
  • 1970-01-01
  • 2023-03-11
  • 1970-01-01
  • 2016-08-14
相关资源
最近更新 更多