猿问

在表单提交上禁用提交按钮

单击后,我编写了以下代码以禁用网站上的提交按钮:


$('input[type=submit]').click(function(){

    $(this).attr('disabled', 'disabled');

});

不幸的是,它没有发送表格。我怎样才能解决这个问题?


编辑 我想绑定提交,而不是表格:)


慕运维8079593
浏览 1181回答 3
3回答

手掌心

特别是如果有人遇到以下问题Chrome:要解决此问题,您需要使用元素中的onSubmit标记<form>设置提交按钮disabled。这将允许Chrome在按下按钮后立即禁用该按钮,并且表单提交仍将继续进行...<form name ="myform" method="POST" action="dosomething.php" onSubmit="document.getElementById('submit').disabled=true;">&nbsp;&nbsp; &nbsp; &nbsp;<input type="submit" name="submit" value="Submit" id="submit">&nbsp;</form>

慕后森

禁用的控件不会提交其值,这不利于了解用户是否单击了保存或删除。因此,我将按钮值存储在确实提交的隐藏值中。隐藏的名称与按钮名称相同。我将所有按钮都称为button。例如 <button type="submit" name="button" value="save">Save</button>基于此我在这里找到了。只需将单击的按钮存储在变量中即可。$(document).ready(function(){&nbsp; &nbsp; var submitButton$;&nbsp; &nbsp; $(document).on('click', ":submit", function (e)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; // you may choose to remove disabled from all buttons first here.&nbsp; &nbsp; &nbsp; &nbsp; submitButton$ = $(this);&nbsp; &nbsp; });&nbsp; &nbsp; $(document).on('submit', "form", function(e)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; var form$ = $(this);&nbsp; &nbsp; &nbsp; &nbsp; var hiddenButton$ = $('#button', form$);&nbsp; &nbsp; &nbsp; &nbsp; if (IsNull(hiddenButton$))&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // add the hidden to the form as needed&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; hiddenButton$ = $('<input>')&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .attr({ type: 'hidden', id: 'button', name: 'button' })&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .appendTo(form$);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; hiddenButton$.attr('value', submitButton$.attr('value'));&nbsp; &nbsp; &nbsp; &nbsp; submitButton$.attr("disabled", "disabled");&nbsp; &nbsp; }});这是我的IsNull功能。使用或替换您自己的IsNull或未定义的版本等。function IsNull(obj){&nbsp; &nbsp; var is;&nbsp; &nbsp; if (obj instanceof jQuery)&nbsp; &nbsp; &nbsp; &nbsp; is = obj.length <= 0;&nbsp; &nbsp; else&nbsp; &nbsp; &nbsp; &nbsp; is = obj === null || typeof obj === 'undefined' || obj == "";&nbsp; &nbsp; return is;}
随时随地看视频慕课网APP
我要回答