$input.disabled = true;
or
$input.disabled = "disabled";
哪一种是标准方式?相反,如何启用禁用的输入?
$input.disabled = true;
or
$input.disabled = "disabled";
哪一种是标准方式?相反,如何启用禁用的输入?
当前回答
禁用:
$('input').attr('readonly', true); // Disable it.
$('input').addClass('text-muted'); // Gray it out with bootstrap.
启用:
$('input').attr('readonly', false); // Enable it.
$('input').removeClass('text-muted'); // Back to normal color with bootstrap.
其他回答
<html>
<body>
Name: <input type="text" id="myText">
<button onclick="disable()">Disable Text field</button>
<button onclick="enable()">Enable Text field</button>
<script>
function disable() {
document.getElementById("myText").disabled = true;
}
function enable() {
document.getElementById("myText").disabled = false;
}
</script>
</body>
</html>
禁用:
$('input').attr('readonly', true); // Disable it.
$('input').addClass('text-muted'); // Gray it out with bootstrap.
启用:
$('input').attr('readonly', false); // Enable it.
$('input').removeClass('text-muted'); // Back to normal color with bootstrap.
// Disable #x
$( "#x" ).prop( "disabled", true );
// Enable #x
$( "#x" ).prop( "disabled", false );
有时您需要禁用/启用表单元素,如input或textarea。Jquery通过将disabled属性设置为“disabled”,帮助您轻松实现这一点。例如:
//To disable
$('.someElement').attr('disabled', 'disabled');
要启用禁用的元素,您需要从该元素中删除“disabled”属性或清空它的字符串。例如:
//To enable
$('.someElement').removeAttr('disabled');
// OR you can set attr to ""
$('.someElement').attr('disabled', '');
参考:http://garmoncheg.blogspot.fr/2011/07/how-to-disableenable-element-with.html
$("input")[0].disabled = true;
or
$("input")[0].disabled = false;
您可以将其放在代码中的全局位置:
$.prototype.enable = function () {
$.each(this, function (index, el) {
$(el).removeAttr('disabled');
});
}
$.prototype.disable = function () {
$.each(this, function (index, el) {
$(el).attr('disabled', 'disabled');
});
}
然后你可以写一些东西,比如:
$(".myInputs").enable();
$("#otherInput").disable();