单击后,我编写了以下代码以禁用网站上的提交按钮:
$('input[type=submit]').click(function(){
$(this).attr('disabled', 'disabled');
});
不幸的是,它没有发送表格。我怎样才能解决这个问题?
编辑 我想绑定提交,而不是表格:)
Answers:
做到onSubmit():
$('form#id').submit(function(){
$(this).find(':input[type=submit]').prop('disabled', true);
});
发生的事情是您实际上在完全触发该提交事件之前禁用了该按钮。
您可能还应该考虑使用ID或CLASS来命名元素,因此不要在页面上选择所有提交类型的输入。
演示: http ://jsfiddle.net/userdude/2hgnZ/
(请注意,我使用preventDefault(),return false因此该表单在示例中并未实际提交;请在使用时将其保留。)
submit()在表单上输入内容,而不是输入内容。观看演示。
特别是如果有人遇到以下问题Chrome:
要解决此问题,您需要使用元素中的onSubmit标签<form>设置提交按钮disabled。这将允许Chrome在按下按钮后立即禁用该按钮,并且表单提交仍将继续进行...
<form name ="myform" method="POST" action="dosomething.php" onSubmit="document.getElementById('submit').disabled=true;">
<input type="submit" name="submit" value="Submit" id="submit">
</form>
.on('submit', function() {..})(或.submit(function() {..}))会更好。即使在这种情况下,您也可能会.submit()从回调内部重新触发表单而陷入无限循环(至少在我正在使用的Opera的最新版本中,该引擎应使用与Chrome相同的引擎)。
禁用的控件不会提交其值,这不利于了解用户是否单击了保存或删除。
所以我将按钮值存储在确实提交的隐藏值中。隐藏的名称与按钮名称相同。我将所有按钮都称为button。
例如 <button type="submit" name="button" value="save">Save</button>
$(document).ready(function(){
var submitButton$;
$(document).on('click', ":submit", function (e)
{
// you may choose to remove disabled from all buttons first here.
submitButton$ = $(this);
});
$(document).on('submit', "form", function(e)
{
var form$ = $(this);
var hiddenButton$ = $('#button', form$);
if (IsNull(hiddenButton$))
{
// add the hidden to the form as needed
hiddenButton$ = $('<input>')
.attr({ type: 'hidden', id: 'button', name: 'button' })
.appendTo(form$);
}
hiddenButton$.attr('value', submitButton$.attr('value'));
submitButton$.attr("disabled", "disabled");
}
});
这是我的IsNull职能。使用或替换您自己的版本为IsNull或undefined等。
function IsNull(obj)
{
var is;
if (obj instanceof jQuery)
is = obj.length <= 0;
else
is = obj === null || typeof obj === 'undefined' || obj == "";
return is;
}
这应该在您的应用程序中解决。
$(":submit").closest("form").submit(function(){
$(':submit').attr('disabled', 'disabled');
});
.prop("disabled", true)改为使用。
您的代码实际上可以在FF上运行,而不能在Chrome上运行。
这适用于FF和Chrome。
$(document).ready(function() {
// Solution for disabling the submit temporarily for all the submit buttons.
// Avoids double form submit.
// Doing it directly on the submit click made the form not to submit in Chrome.
// This works in FF and Chrome.
$('form').on('submit', function(e){
//console.log('submit2', e, $(this).find('[clicked=true]'));
var submit = $(this).find('[clicked=true]')[0];
if (!submit.hasAttribute('disabled'))
{
submit.setAttribute('disabled', true);
setTimeout(function(){
submit.removeAttribute('disabled');
}, 1000);
}
submit.removeAttribute('clicked');
e.preventDefault();
});
$('[type=submit]').on('click touchstart', function(){
this.setAttribute('clicked', true);
});
});
</script>
如何禁用提交按钮
只需在onclick事件上调用函数,然后...返回true即可提交,返回false即可禁用提交。 或者 在window.onload上调用一个函数,例如:
window.onload = init();
并在init()中执行以下操作:
var theForm = document.getElementById(‘theForm’);
theForm.onsubmit = // what ever you want to do
以下为我工作:
var form_enabled = true;
$().ready(function(){
// allow the user to submit the form only once each time the page loads
$('#form_id').on('submit', function(){
if (form_enabled) {
form_enabled = false;
return true;
}
return false;
});
});
如果用户尝试多次提交表单(通过单击提交按钮,按Enter等),则这将取消提交事件。
我一直在使用blockUI来避免浏览器在禁用或隐藏的按钮上不兼容。
http://malsup.com/jquery/block/#element
然后我的按钮有一个类自动按钮:
$(".autobutton").click(
function(event) {
var nv = $(this).html();
var nv2 = '<span class="fa fa-circle-o-notch fa-spin" aria-hidden="true"></span> ' + nv;
$(this).html(nv2);
var form = $(this).parents('form:first');
$(this).block({ message: null });
form.submit();
});
然后是这样的形式:
<form>
....
<button class="autobutton">Submit</button>
</form>
如果您使用的是类型为提交的按钮,并且还希望提交按钮的值(如果禁用了按钮则不会发生),则可以设置表单数据属性,然后进行测试。
// Add class disableonsubmit to your form
$(document).ready(function () {
$('form.disableonsubmit').submit(function(e) {
if ($(this).data('submitted') === true) {
// Form is already submitted
console.log('Form is already submitted, waiting response.');
// Stop form from submitting again
e.preventDefault();
} else {
// Set the data-submitted attribute to true for record
$(this).data('submitted', true);
}
});
});
$('input[type=submit]').submit(function():已发送表单,但该按钮将不再禁用...