admin管理员组文章数量:1414621
I'm trying to disable a button after a user clicks it. I have this functionality working great but now the form wont submit... Here is my javascript:
<script>
$('.button').on('click', function(event) {
$(this).prop('disabled', true);
});
</script>
Again this disables the button nicely but now my form wont submit. Is this a well known problem?
I'm trying to disable a button after a user clicks it. I have this functionality working great but now the form wont submit... Here is my javascript:
<script>
$('.button').on('click', function(event) {
$(this).prop('disabled', true);
});
</script>
Again this disables the button nicely but now my form wont submit. Is this a well known problem?
Share Improve this question asked Jun 5, 2018 at 20:46 BitwiseBitwise 8,47125 gold badges80 silver badges177 bronze badges 1- I cannot replicate this: jsfiddle/khrismuc/2zo6uev5 – user5734311 Commented Jun 5, 2018 at 21:05
3 Answers
Reset to default 5Instead of putting it in the click
handler, put it in the form's submit
handler.
$('form').on('submit', function(event) {
$(this).find(".button").prop('disabled', true);
});
<script src="https://ajax.googleapis./ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form>
<button class="button">Submit</button>
</form>
Click
event occurs before form submit
and disabled
button prevents submission. This is workaround:
<script>
$('.button').on('click', function(event) {
$this=$(this);
setTimeout(function(){$this.prop('disabled', true);},50);
});
</script>
Another workaround is to process $('form').on('submit',function(){...});
Edit: ...INSTEAD.
<script>
$('.button').on('click', function(event) {
$(this).prop('disabled', true);
$(this).parents('form').submit();
});
</script>
本文标签: javascriptdisabling button on click stop form from submittingStack Overflow
版权声明:本文标题:javascript - disabling button on click stop form from submitting - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745164125a2645590.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论