钥匙功能和表单提交
我具有一个功能,其中文本框上有一个键。一旦我按了“ 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.
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
添加一个
keydown
处理程序,该处理程序可以防止's'添加:因此您的整个脚本变为:
Add a
keydown
handler which prevents 's' from being added:So your whole script becomes:
如果您的输入仅接受数字,请使用Regex:
/[A-ZA-Z]/G
和.replace()
,因此您过滤了字母。如果您想要所有内容,但是's' REGEX:/[ss]/g
(所有字母的替换过滤器都会评论)。顺便说一句,除非
&lt; button&gt;
具有type =“ submit”
或notype
完全无法提交(您可能已经已经已经已经已经已经过去了)知道这一点,但需要对未来的读者说)。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>
hastype="submit"
or notype
at all (you probably already know that, but it needed to be said for future readers).