如何禁用页面上的所有按钮和链接,但仍允许回发?
我试图阻止用户单击页面上多个导致回发的元素。换句话说,如果他们单击“继续”提交按钮,他们不应该能够在原始请求返回之前导致另一次回发。
我尝试过两个版本的 jQuery 代码。两者都不完全符合我的要求:
此版本将禁用所有回发元素,但这样做会阻止单击的元素触发。根据记录,我不认为 .removeAttr('onclick')
确实是必需的,但将其排除似乎并没有改变行为。
$(function() {
$('a, :button, :submit').click(function() {
var elements = $('a, :button, :submit');
elements.attr('disabled', 'disabled').removeAttr('onclick');
});
});
此版本禁用所有其他回发元素,但它允许我重用被单击的同一元素 - 我不希望能够两次点击同一按钮。
$(function() {
$('a, :button, :submit').click(function() {
var otherelements = $('a:not(#' + $(this).attr('id') + '), :button:not(#' + $(this).attr('id') + '), :submit:not(#' + $(this).attr('id') + ')');
elements.attr('disabled', 'disabled').removeAttr('onclick');
});
});
我怎样才能防止这种行为?
I'm trying to prevent the user from clicking on more than one postback-causing element on the page. In other words, if they click the 'continue' submit button, they shouldn't be able to cause another postback before the original request comes back.
I've had a go with two versions of jQuery code. Neither does exactly what I want:
This version will disable all the postback elements, but in doing so, it stops the clicked element from firing. For the record, I don't think the .removeAttr('onclick')
is really required, but leaving it out doesn't seem to change the behaviour.
$(function() {
$('a, :button, :submit').click(function() {
var elements = $('a, :button, :submit');
elements.attr('disabled', 'disabled').removeAttr('onclick');
});
});
This version disables all other postback elements, but it lets me reuse the same element that was clicked - I don't want to be able to hit the same button twice.
$(function() {
$('a, :button, :submit').click(function() {
var otherelements = $('a:not(#' + $(this).attr('id') + '), :button:not(#' + $(this).attr('id') + '), :submit:not(#' + $(this).attr('id') + ')');
elements.attr('disabled', 'disabled').removeAttr('onclick');
});
});
How can I prevent this behaviour?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我刚刚在没有 JQuery 的情况下测试了你的第一种方法,它工作得很好,即禁用提交按钮并没有阻止表单提交。
也许您想仔细检查是否还有其他情况,例如 JQuery,正在发生?
I just tested your first approach without JQuery, and it worked fine, i.e. disabling the submit button didn't prevent the form submission.
Maybe you want to double check is there is anything else, e.g. JQuery, going on?
也许您可以放置一个标志或其他东西,让它记住单击了哪个按钮,如果该标志存在,您可以删除该回发元素上的 onclick 事件。但我认为这不能单独在客户端脚本中完成,因为一旦提交页面,所有客户端元素和脚本都会刷新。
Maybe you could put a flag or something that it could remember what button it was clicked and if that flag exist, you can remove the onclick event on that postback-causing element. But I think this cannot be done in client side scripting alone, since once the page is submitted, all client side elements and scripts are refreshed.
也许不要将其设置为单击函数,而是将其设置为 onmouseup,以便它在单击事件发生后触发。
Perhaps instead of making this a click function make it onmouseup so it fires after the click event has occured.
这是一个有效的最终版本 - 只是覆盖表单提交事件而不是查看任何单独的元素。
感谢大家的建议。
Here's a final version that worked - just overriding the form submit event rather than looking at any individual elements.
Thanks all for your suggestions.