钥匙功能和表单提交

发布于 2025-02-09 18:21:31 字数 1065 浏览 1 评论 0原文

我具有一个功能,其中文本框上有一个键。一旦我按了“ S”键,表格就会提交。但是问题是,每当我按“ S”键时,它都会添加到文本框中,并且包含在文本框值中,并且已提交。在按“ S”时,我该如何避免它,不应将其包含在文本框中。只需在文本框中进行提交而无需s即可。

正如您在我的图像中看到的那样,最后一个值有一个“ S”。我的目标是避免每当我使用“ S”键提交表单时。

谢谢

     $(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>

I have a function where I have a keyup on my textbox. Once I hit the "S" key and the form will submit. 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. How can I avoid it when pressing "S", it should not be included in the textbox. Just do the submit without S value in textbox.

As you can see here in my image, there's a "S" on the last value. My target is to avoid that whenever I'm submitting my form using "S" key.

enter image description here

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>

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

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

发布评论

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

评论(2

迷途知返 2025-02-16 18:21:31

添加一个keydown处理程序,该处理程序可以防止's'添加:

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

因此您的整个脚本变为:

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

Add a keydown handler which prevents 's' from being added:

$('.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();
        
        }
    });
});
╰つ倒转 2025-02-16 18:21:31

如果您的输入仅接受数字,请使用Regex:/[A-ZA-Z]/G.replace(),因此您过滤了字母。如果您想要所有内容,但是's' REGEX:/[ss]/g(所有字母的替换过滤器都会评论)。

顺便说一句,除非&lt; button&gt;具有type =“ submit”或no 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>

If your input accepts only numbers, use Regex: /[a-zA-Z]/g and .replace() so you filter out letters. If you want everything but 's' Regex: /[sS]/g (Replacement filter for all letters is commented).

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).

$(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>

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文