按钮 onclick 工作正常,但按 Enter 提交表单时出现不良行为

发布于 2024-11-26 11:26:59 字数 1130 浏览 0 评论 0原文

我不确定是否需要一个表单,它是一个 javascript,它从文本框中获取输入,对其进行一些计算,然后在 div 中显示结果。我只希望用户能够通过单击“提交”或按 Enter 键来提交。

简而言之,如果按钮类型是“按钮”,则单击工作正常,但按 Enter 工作很奇怪,如果按钮类型是“提交”,按 Enter 工作正常,但单击工作很奇怪。下面是代码示例(删除了一些工作正常的业务逻辑和其他变量,以便更简单地在此处显示):

<form action="#" onsubmit="calcserial()">
Serial no: <input type="text" name="serialno" id="serialno" /><br />
<input type="button" value="Submit" onclick="calcserial()" />
</form>

<script type="text/javascript">
function calcserial(){  

var x=document.getElementById('serialno').value;

var year=[business logic];
var month=[business logic];

document.getElementById('results').innerHTML="Shipped: " + month + ", " + year;  
} 
</script>

<div id=results></div>

为了提供有关奇怪行为的更多详细信息,当输入类型为“按钮”并且我按 Enter 键时,而不是与运行 calcserial() 函数相比,文本框被清除,并且 url 更改为:

[filename].php?serialno=[value]#

[value] 为文本框中的内容。然后,如果我再次输入相同的精确值并再次按 Enter 键,它就会按预期工作,但前提是 url 已经显示了我正在提交的值。如果我输入不同的值,文本框将再次被清除,并且 url 会更改以显示新值。如果我将输入类型切换为“提交”,则按 Enter 键的问题得到解决,但单击按钮提交时会出现同样的问题。

为什么会发生这种情况?有什么想法如何解决吗?谢谢!

I'm not sure if I need a form for this, it's a javascript that takes an input from a textbox, does some calculation on it, and displays the results in a div. I just want the user to be able to submit by clicking submit or by pressing enter.

In short, if the button type is "button", clicking works fine but pressing enter works strangely, and if the button type is "submit", pressing enter works fine, but clicking works strangely. Below is a sample of the code (removed some business logic and other variables that were working fine to make it simpler to display here):

<form action="#" onsubmit="calcserial()">
Serial no: <input type="text" name="serialno" id="serialno" /><br />
<input type="button" value="Submit" onclick="calcserial()" />
</form>

<script type="text/javascript">
function calcserial(){  

var x=document.getElementById('serialno').value;

var year=[business logic];
var month=[business logic];

document.getElementById('results').innerHTML="Shipped: " + month + ", " + year;  
} 
</script>

<div id=results></div>

To give more detail about the strange behavior, when input type is "button" and I press enter, rather than running the calcserial() function, the textbox is cleared and the url is changed to:

[filename].php?serialno=[value]#

With [value] being what ever was in the text box. Then, if I put in the same exact value again and press enter again, it works as intended, but only if the url already shows the value I am submitting. If I enter a different value, the text box is cleared again and the url is changed to show the new value. If I switch the input type to "submit", the problem is resolved for pressing enter, but the same problem then occurs when clicking the button to submit.

Why is this happening? Any ideas how to resolve it? Thanks!

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

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

发布评论

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

评论(1

仲春光 2024-12-03 11:26:59

尝试更改此:

改为:

这应该会停止通常在提交表单时发生的默认操作,在您的情况下,使用 action="#" 会导致重新加载同一页面。

鉴于您实际上不想提交任何内容(到网络服务器),因此您实际上并不需要表单。 加上检查文本输入的 Enter onkeyup 可以达到相同的效果。

Try changing this:

<form action="#" onsubmit="calcserial()">

To this:

<form action="#" onsubmit="calcserial();return false;">

That should stop the default action that would normally happen on submit of a form, which in your case with action="#" results in the same page being reloaded.

Given that you don't actually want to submit anything (to the webserver) you don't really need a form for this. An <input type="button"> plus checking for enter onkeyup of the text input could achieve the same effect.

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