单击提交按钮后如何禁用它?
我在表单末尾有一个提交按钮。
我在提交按钮中添加了以下条件:
onClick="this.disabled=true;
this.value='Sending…';
this.form.submit();"
但是当它移动到下一页时,参数没有传递并且传递了空值。
I have a submit button at the end of the form.
I have added the following condition to the submit button:
onClick="this.disabled=true;
this.value='Sending…';
this.form.submit();"
But when it moves to the next page, the parameters did not pass and null values are passed.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(19)
您应该首先提交表单,然后更改提交的值:
You should first submit your form and then change the value of your submit:
您可能提交了两次表格。
删除
this.form.submit()
或在末尾添加return false
。你应该最终得到
onClick="this.disabled=true; this.value='Sending…';"
Probably you're submitting the form twice.
Remove the
this.form.submit()
or addreturn false
at the end.you should end up with
onClick="this.disabled=true; this.value='Sending…';"
在 IE11、FF53、GC58 上测试:
tested on IE11, FF53, GC58 :
您需要在
注意:如果您的表单元素具有
required 属性将起作用。
You need to disable the button in the
onsubmit
event of the<form>
:Note: this way if you have a form element which has the
required
attribute will work.当您提交表单时,禁用的 HTML 表单元素不会与 post/get 值一起发送。因此,如果您在单击后禁用提交按钮,并且该提交按钮设置了
name
属性,则它不会在 post/get 值中发送,因为该元素现已禁用。这是正常行为。解决此问题的方法之一是使用隐藏的表单元素。
Disabled HTML forms elements aren't sent along with the post/get values when you submit the form. So if you disable your submit button once clicked and that this submit button have the
name
attribute set, It will not be sent in the post/get values since the element is now disabled. This is normal behavior.One of the way to overcome this problem is using hidden form elements.
诀窍是延迟禁用按钮,然后提交您可以使用的表单
window.setTimeout('this.disabled=true',0);
是的,即使 0 MS 也能工作
the trick is to delayed the button to be disabled, and submit the form you can use
window.setTimeout('this.disabled=true',0);
yes even with 0 MS is working
使用 JQuery,你可以做到这一点..
Using JQuery, you can do this..
如果禁用该按钮,则其名称=值对确实不会作为参数发送。但是应该发送剩余的参数(只要它们各自的输入元素和父表单没有被禁用)。您可能只测试按钮或其他输入字段甚至表单被禁用?
If you disable the button, then its name=value pair will indeed not be sent as parameter. But the remnant of the parameters should be sent (as long as their respective input elements and the parent form are not disabled). Likely you're testing the button only or the other input fields or even the form are disabled?
下面是一个扩展了 Andreas Köberle 解决方案的简单示例。它使用 jQuery 作为事件处理程序和文档就绪事件,但这些可以切换为纯 JS:
然后可以在 HTML 中使用,如下所示:
Here's a drop-in example that expands on Andreas Köberle's solution. It uses jQuery for the event handler and the document ready event, but those could be switched to plain JS:
It can then be used in HTML like this:
我认为您不需要
this.form.submit()
。禁用代码应该运行,然后它将传递单击表单的单击。I don't think you need
this.form.submit()
. The disabling code should run, then it will pass on the click which will click the form.我使用的另一个解决方案是移动按钮而不是禁用它。在这种情况下,您就不会遇到那些“禁用”问题。
最后,您真正想要的是人们不要按两次,如果按钮不存在,他们就无法执行此操作。
您也可以将其替换为其他按钮。
Another solution i´ve used is to move the button instead of disabling it. In that case you don´t have those "disable" problems.
Finally what you really want is people not to press twice, if the button is not there they can´t do it.
You may also replace it with another button.
您的问题很混乱,您确实应该发布一些代码,但这应该有效:
我认为当您使用 this.form.submit() 时,它会执行单击提交按钮时自然发生的事情。如果您想要同一页面提交,您应该考虑在
submitForm()
方法中使用 AJAX(见上文)。此外,在
onClick
属性值末尾返回false
会禁止触发默认事件(在本例中是提交表单)。Your question is confusing and you really should post some code, but this should work:
I think that when you use
this.form.submit()
it's doing what happens naturally when you click the submit button. If you want same-page submit, you should look into using AJAX in thesubmitForm()
method (above).Also, returning
false
at the end of theonClick
attribute value suppresses the default event from firing (in this case submitting the form).包装代码以在 div 中显示
id=WAIT style="display:none">要显示的文本(结束 div)
将代码隐藏在 div 中
id=BUTTONS style="display:inline"> ...按钮或任何隐藏的东西
onclick="showwait();"
(结束分区)
wrap code to show in a div
id=WAIT style="display:none"> text to display (end div)
wrap code to hide in a div
id=BUTTONS style="display:inline"> ... buttons or whatever to hide with
onclick="showwait();"
(end div)
就我而言,这是需要的。
禁用表单提交上的提交按钮
它在没有它的 Internet Explorer 和 Firefox 中工作正常,但它在 Google Chrome 中不起作用。
问题是您在按钮实际触发提交事件之前禁用了该按钮。
In my case this was needed.
Disable submit button on form submit
It works fine in Internet Explorer and Firefox without it, but it did not work in Google Chrome.
The problem is that you are disabling the button before it can actually trigger the submit event.
我认为禁用按钮的简单方法是
:data => {disable_with:“正在保存..”}
这将提交一个表单,然后禁用一个按钮,如果您有任何像
required = 'required' 这样的验证,它也不会禁用按钮。
I think easy way to disable button is
:data => { disable_with: "Saving.." }
This will submit a form and then make a button disable, Also it won't disable button if you have any validations like
required = 'required'.
在此工作示例中,用户在 JavaScript 中确认他确实想要中止。如果为 true,则禁用该按钮以防止双击,然后将运行更新数据库后面的代码。
我遇到了问题,因为 .net 可以更改按钮的名称
,因为您正在使用 OnClientClick 覆盖 OnClick,即使您的验证方法成功,后面的代码也不会工作。这就是为什么您将 UseSubmitBehavior 设置为 false 以使其正常工作
PS:如果您的代码位于 vb.net 中,则不需要 OnClick!
In this working example, the user confirms in JavaScript that he really wants to abort. If true, the button is disabled to prevent double click and then the code behind which updates the database will run.
I had issues because .net can change the name of the button
Because you are overriding the OnClick with OnClientClick, even if your validation method succeeds, the code behind wont work. That's why you set UseSubmitBehavior to false to make it work
PS: You don't need the OnClick if your code is in vb.net!
好吧,我做了很多研究如何让这项工作完美地进行。
因此,最好的选择是创建一个设置超时来禁用按钮 onclick。
现在,当后端运行提交函数时,就会出现问题。然后事件堆积在队列中,每当 javascript
"button.disabled == true"
添加到 onclick 事件时,只有第一个操作(即禁用按钮)被触发,而不是提交在后端运行的操作(此后端提交函数可以包含任何内容,例如 $.ajax)。对于单击时禁用单个按钮:
此代码将禁用您的按钮,并且还会在队列上运行该函数。 timeout = 0 实际上用于触发后续后端任务。
用于禁用屏幕中的所有按钮:
这将帮助您禁用页面中存在的所有按钮(只需根据您的用例使用它)。
此外,这个(禁用按钮)是 settimeout=0 的一个很好的用例,功能描述,因为它将“推迟”调用,直到当前“堆叠的 javascript 事件”完成。
谢谢您,希望这对将来的人有帮助。
Okay, I did a lot of research on how to make this work perfectly.
So the best option is to create a set timeout for disabling a button onclick.
Now, the problem arise when there is a submit function running on the backend. Then the events become stacked in a queue and whenever the javascript
"button.disabled == true"
is added to the onclick event, only the first action(i.e. disabling the button) gets triggered and not the submit action which is running in the backend (this backend submit function can comprise of anything such as $.ajax).For disabling Single button on click :
This code will disable your button and also would run the function on the queue. timeout = 0 actually is used for firing subsequent backend tasks.
For disabling all btns in the screen :
This would help you disable all of the buttons present in the page (just use it according to your usecase).
Also, this (disabled button) is a good use case for settimeout=0, functionality description as it will "defer" the call until the currently "stacked javascript events" are finished.
Thank you and hope this helps someone's in the future.
我成功了。当设置超时时,它可以正常工作并发送所有值。
I did the trick. When set timeout, it works perfectly and sending all values.