当我按下 Enter 键时,按键事件不会被触发

发布于 2025-01-08 17:00:22 字数 711 浏览 2 评论 0原文

我已将按键事件附加到我的文本框。当用户按下其他键时,我正在做一些处理工作,但是当用户按下 Enter 键时,我正在将文本框中的值提交到某个服务器。我能够完成所有处理,一切都很好,但是当我按 Enter 键时,事件不会被触发。所以,我无法将我的值提交到服务器。

这是我的代码:

$("#txt" + filterID).keypress(txtInput_keypress);

function txtInput_keypress(e) { 
    var code = (e.keyCode ? e.keyCode : e.which);
    var strValue = $(this).val()+ String.fromCharCode(e.which);
    var bool = $.trim(strValue).match(reg); 
    if (code == 13) {  
        //textbox value submission code
    }
    else if (parseFloat($.trim(strValue)) > max) {
        e.preventDefault();
    }
    else if (bool) {  
        return true;
    }
    else {
        e.preventDefault();
    }  
} 

我的代码有什么问题吗?请有人帮我解决这个问题。

I have attached a keypress event to my textbox. when the user presses other keys i am doing some processing work, but when the user presses the Enter key i am submitting the value in the textbox to some server. I am able to do all processing and every thing is fine, but when i press enter key the event is not getting fired. so, i ma unable to submit my value to the server.

Here is my code:

$("#txt" + filterID).keypress(txtInput_keypress);

function txtInput_keypress(e) { 
    var code = (e.keyCode ? e.keyCode : e.which);
    var strValue = $(this).val()+ String.fromCharCode(e.which);
    var bool = $.trim(strValue).match(reg); 
    if (code == 13) {  
        //textbox value submission code
    }
    else if (parseFloat($.trim(strValue)) > max) {
        e.preventDefault();
    }
    else if (bool) {  
        return true;
    }
    else {
        e.preventDefault();
    }  
} 

whats wrong with my code? Please somebody help me to fix this issue.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

谜兔 2025-01-15 17:00:22
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
    <script src="Scripts/jquery-1.7.1.min.js" type="text/javascript"></script>
    <script type="text/javascript">
        $(document).ready(function () {
            var filterID = 1, reg = '^([0-9]|[1-9][0-9]|[1-9][0-9][0-9])
, max = 1000;

            $("#txt" + filterID).keypress(txtInput_keypress);

            function txtInput_keypress(e) {
                var code = (e.keyCode ? e.keyCode : e.which);
                var strValue = $(this).val() + String.fromCharCode(e.which);
                var bool = $.trim(strValue).match(reg);
                if (code == 13) {
                    //textbox value submission code
                    //$('form#test').submit(); // if alert is not coming uncoment this line.
                }
                else if (parseFloat($.trim(strValue)) > max) {
                    e.preventDefault();
                }
                else if (bool) {
                    return true;
                }
                else {
                    e.preventDefault();
                }
            }
        });                
    </script>
</head>
<body>
    <form id="test" action="javascript:alert('success!');">
    <input type="text" id="txt1" />
    </form>
</body>
</html>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
    <script src="Scripts/jquery-1.7.1.min.js" type="text/javascript"></script>
    <script type="text/javascript">
        $(document).ready(function () {
            var filterID = 1, reg = '^([0-9]|[1-9][0-9]|[1-9][0-9][0-9])
, max = 1000;

            $("#txt" + filterID).keypress(txtInput_keypress);

            function txtInput_keypress(e) {
                var code = (e.keyCode ? e.keyCode : e.which);
                var strValue = $(this).val() + String.fromCharCode(e.which);
                var bool = $.trim(strValue).match(reg);
                if (code == 13) {
                    //textbox value submission code
                    //$('form#test').submit(); // if alert is not coming uncoment this line.
                }
                else if (parseFloat($.trim(strValue)) > max) {
                    e.preventDefault();
                }
                else if (bool) {
                    return true;
                }
                else {
                    e.preventDefault();
                }
            }
        });                
    </script>
</head>
<body>
    <form id="test" action="javascript:alert('success!');">
    <input type="text" id="txt1" />
    </form>
</body>
</html>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文