简体   繁体   English

带表单提交的 keyup 功能

[英]keyup function with form submit

I have a function where I have a keyup on my textbox.我有一个功能,我的文本框上有一个键。 Once I hit the "S" key and the form will submit.一旦我按下“S”键,表格就会提交。 But the problem is, whenever I press the "S" key, it adds to the textbox and it is included in the textbox value and it is submitted.但问题是,每当我按下“S”键时,它都会添加到文本框中,并包含在文本框值中并提交。 How can I avoid it when pressing "S", it should not be included in the textbox.按“S”时如何避免它,它不应该包含在文本框中。 Just do the submit without S value in textbox.只需在文本框中提交不带 S 值的提交即可。

As you can see here in my image, there's a "S" on the last value.正如您在我的图片中看到的那样,最后一个值上有一个“S”。 My target is to avoid that whenever I'm submitting my form using "S" key.我的目标是在我使用“S”键提交表单时避免这种情况。

在此处输入图像描述

Thank you谢谢

 $(document).ready(function() { $('.btnsub').keyup(function(event) { if (event.which === 83) { event.preventDefault(); $('#insertEntryy').click(); console.log('1'); } }); });
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <form action="blabla"> <input type="text" class="btnsub"> <button id="insertEntryy" type="button"> asd </button> </form>

Add a keydown handler which prevents 's' from being added:添加一个keydown处理程序,以防止添加“s”:

$('.btnsub').keydown(function(event) {
    if (event.which === 83)
    {
        event.preventDefault();  
    }
});

So your whole script becomes:所以你的整个脚本变成:

$(document).ready(function() {
    $('.btnsub').keyup(function(event) {
        if (event.which === 83)
        {
            event.preventDefault();
            event.stopPropagation();
            $('#insertEntryy').click();
            console.log('1');
        
        }
    });

    // New handler:
    $('.btnsub').keydown(function(event) {
        if (event.which === 83)
        {
            event.preventDefault();
        
        }
    });
});

If your input accepts only numbers, use Regex: /[a-zA-Z]/g and .replace() so you filter out letters.如果您的输入仅接受数字,请使用正则表达式: /[a-zA-Z]/g.replace() ,以便过滤掉字母。 If you want everything but 's' Regex: /[sS]/g (Replacement filter for all letters is commented).如果您想要除 's'正则表达式之外的所有内容: /[sS]/g (所有字母的替换过滤器均已注释)。

BTW, the form can't submit unless the <button> has type="submit" or no type at all (you probably already know that, but it needed to be said for future readers).顺便说一句,除非<button>具有type="submit"或根本没有type ,否则表单无法提交(您可能已经知道,但需要为未来的读者说一下)。

 $(document).ready(function() { $('.btnsub').on('keyup', function(e) { const rgx = new RegExp(/[sS]/, 'g'); // const rgx = new RegExp(/[a-zA-Z]/, 'g') if (rgx.test(this.value)) { this.value = this.value.replace(rgx,''); } if (e.key === 's') { $('#insertEntryy').click(); console.log('clicked'); } console.log(this.value); }); });
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <form action="blabla"> <input type="text" class="btnsub"> <button id="insertEntryy" type='button'> asd </button> </form>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM