在 jquery 中禁用和启用提交按钮
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14170809/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Disable and Enable submit button in jquery
提问by Rohitashv Singhal
I am using jquery to disable the submit button and load loading image,
我正在使用 jquery 禁用提交按钮并加载加载图像,
In submit button I am using the folowing :
在提交按钮中,我使用以下内容:
<div id="registerbtn" name="registerbtn">
<input type="submit" class="btn btn-primary" icon="ok white" value="Register" id="register" name="register"/>
</div>
<div class="span4" style="display:none;" id="loadingtext">
<?php echo $imghtml=CHtml::image('images/loading.gif');?>
</div>
and in JQuery, I am using following code :
在 JQuery 中,我使用以下代码:
$(document).ready(function() {
$('#register').click(function() {
$('input[type="submit"]').attr('disabled','disabled');
});
$("#loadingtext").show();
});
});
When I do this, then this button is disabled permanently, but if I want to remove then what should I do ??
当我这样做时,此按钮将被永久禁用,但如果我想删除,我该怎么办?
回答by Roko C. Buljan
Use .prop()
method and use the $(this)
to reference the target element within the callback function
使用.prop()
方法并使用$(this)
来引用回调函数中的目标元素
jQuery(function($) {
const $register = $("#register"),
$loading = $("#loading");
$register.on("click", function() {
$(this).prop('disabled', true);
$loading.show();
setTimeout(function() {
$register.prop('disabled', false);
$loading.hide();
}, 2000);
});
});
<input id="register" value="Register" type="submit">
<div id="loading" style="display:none;">Wait 2 sec...</div>
<script src="//code.jquery.com/jquery-3.1.0.js"></script>
回答by Olaf Dietsche
This is answered in the jQuery FAQ
这在jQuery 常见问题解答中得到了回答
How do I disable/enable a form element?
There are two ways to disable/enable form elements.
如何禁用/启用表单元素?
有两种方法可以禁用/启用表单元素。
Set the 'disabled' attribute to true or false:
将 'disabled' 属性设置为 true 或 false:
// Disable #x
$('#x').attr('disabled', true);
// Enable #x
$('#x').attr('disabled', false);
Add or remove the 'disabled' attribute:
添加或删除 'disabled' 属性:
// Disable #x
$("#x").attr('disabled', 'disabled');
// Enable #x
$("#x").removeAttr('disabled');
Update:Now the way to do it (as said by FAQ) is only:
更新:现在的方法(如常见问题所述)只是:
// Disable #x
$( "#x" ).prop( "disabled", true );
// Enable #x
$( "#x" ).prop( "disabled", false );
回答by PassKit
To permanently remove the button
永久删除按钮
$('#register').remove();
To restore the button
恢复按钮
$('#register').attr('disabled',false);
回答by Ignacio Correia
Is $('#divID').remove();
what you're looking for?
是$('#divID').remove();
你要找的吗?