admin 管理员组文章数量: 1087134
I am constructing an HTML button object inside a function. I want to enable or disable the button depending on a condition from this function's parameter. I have simplified my problem to the code below:
function test(condition) {
var html = ""
var enableBtn = false;
if (condition == true) {
enableBtn = true;
} else {
enableBtn = false;
}
html += '<button type="button" disabled="' + enableBtn + '"> Click </button></div>';
$("#testLocation").html(html);
}
The problem here is that the disabled
flag does not work with true or false. According to the official HTML5 specs:
.html#enabling-and-disabling-form-controls:-the-disabled-attribute
.html#boolean-attribute,
only valid options for disabled
are
<input type="text" disabled />
<input type="text" disabled="" />
<input type="text" disabled="disabled" />
So how do I get around this? I also tried setting disabled = null
but that does not work.
I am constructing an HTML button object inside a function. I want to enable or disable the button depending on a condition from this function's parameter. I have simplified my problem to the code below:
function test(condition) {
var html = ""
var enableBtn = false;
if (condition == true) {
enableBtn = true;
} else {
enableBtn = false;
}
html += '<button type="button" disabled="' + enableBtn + '"> Click </button></div>';
$("#testLocation").html(html);
}
The problem here is that the disabled
flag does not work with true or false. According to the official HTML5 specs:
https://www.w3/TR/html5/forms.html#enabling-and-disabling-form-controls:-the-disabled-attribute
https://www.w3/TR/html5/infrastructure.html#boolean-attribute,
only valid options for disabled
are
<input type="text" disabled />
<input type="text" disabled="" />
<input type="text" disabled="disabled" />
So how do I get around this? I also tried setting disabled = null
but that does not work.
-
6
if (condition = true)
should beif (condition == true)
– j08691 Commented May 1, 2017 at 19:25 - 1 just set enableBtn = "disabled" or enableBtn = "". – Karthik Ganesan Commented May 1, 2017 at 19:27
- in case your button have id attribute you can directly use jquery attr('disabled',false) or set it to true – RohitS Commented May 1, 2017 at 19:27
- @j08691 thanks for catching that, was a typo – OneMoreQuestion Commented May 1, 2017 at 19:28
-
if(condition) {$el.attr('disabled', 'disabled');} else {$el.removeAttr('disabled')}
? – Gogol Commented May 1, 2017 at 20:13
5 Answers
Reset to default 2You can also use empty string if condition is not true and assign enableBtn
to disabled
if true.
function test(condition) {
var html = ""
var enableBtn = '';
if (condition == true) {
enableBtn = 'disabled';
} else {
enableBtn = '';
}
html += '<button type="button" ' + enableBtn + '> Click </button></div>';
$("#testLocation").html(html);
}
$('#test').on('click', function() {
test(true)
});
<script src="https://ajax.googleapis./ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button id="test">Test button</button>
<div id="testLocation"></div>
Disabled is a "boolean attribute," which is just a fancy spec way of saying that the attribute is considered "true" if present (whether you use disabled="spaghetti"
or disabled="disabled"
is irrelevant), and "false" if absent. Thus, you actually need to toggle whether or not the attribute is added to your HTML element, instead of simply changing its value.
From the page you linked:
Note: The values "true" and "false" are not allowed on boolean attributes. To represent a false value, the attribute has to be omitted altogether.
function test(condition) {
$('#testLocation').html(
'<button type="button" ' + (condition ? 'disabled' : '') + '>Click</button></div>'
)
}
test(true)
<script src="//ajax.googleapis./ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="testLocation"></div>
add/remove disabled
attribute
$(input).attr('disabled','disabled')
$(input).removeAttr('disabled')
Try below code.
function test(condition) {
var btn = $('<button type="button"> Click </button></div>');
if (condition == true) {
btn.attr('disabled','disabled')
} else {
// btn.removeAttr('disabled') dont need this..we will add disabled only if required.
}
$("#testLocation").html(btn);
}
Or you could do like this
function test(condition) {
$("#testLocation").html('<button type="button" '+(condition?'disabled':'')+'> Click </button></div>');
}
Try these..
$('#input').prop('disabled', true);
$('#input').prop('disabled', false);
This always works for me when I want to toggle disabled
:
$("#testLocation").setAttribute("disabled, "");
Very straightforward. And if you want to remove it,
$("#testLocation").removeAttribute("disabled");
This does not require a second parameter.
本文标签: javascriptHow to enable and disable HTML39s disabled propertyStack Overflow
版权声明:本文标题:javascript - How to enable and disable HTML's disabled property - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://roclinux.cn/p/1744098989a2533409.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论