admin管理员组文章数量:1415491
I am disabling a button until atleast one checkbox has been selected like so:
var checkboxes = $("input[type='checkbox']"),
submitButt = $("input[type='button']");
checkboxes.click(function() {
submitButt.attr("disabled", !checkboxes.is(":checked"));
});
My buttons initial state is disabled but I need it to be like faded out or opacity added to it, im not sure what the best approach would be either do it with adding a class or just adding CSS? im not really sure?
I am disabling a button until atleast one checkbox has been selected like so:
var checkboxes = $("input[type='checkbox']"),
submitButt = $("input[type='button']");
checkboxes.click(function() {
submitButt.attr("disabled", !checkboxes.is(":checked"));
});
My buttons initial state is disabled but I need it to be like faded out or opacity added to it, im not sure what the best approach would be either do it with adding a class or just adding CSS? im not really sure?
Share Improve this question asked Feb 4, 2017 at 10:57 danjbhdanjbh 61510 silver badges21 bronze badges 5- It's not at all clear what you're asking. Are you trying to make the button look disabled when it isn't disabled, or...? – T.J. Crowder Commented Feb 4, 2017 at 10:59
-
Side note: Although jQuery will fix it for you if you do what's above, the correct way to set the
disabled
property is either.prop("disabled", booleanFlagHere)
, or if you want to useattr
then setting it to any string to set the flag or usingremoveAttr
to remove it. (Butprop
is simpler.) – T.J. Crowder Commented Feb 4, 2017 at 11:00 - @T.J.Crowder no the button IS disabled UNTIL a checkbox has been checked/selected. So the button is to appear disabled and then appear enabled once the action has taken place. – danjbh Commented Feb 4, 2017 at 11:00
-
1
To style a disabled element, you can use the following CSS selectors
elementTag:disabled {/* CSS */}
orelementTag[disabled] {/* CSS */}
but as T.J.Crowder noted: it's difficult to interpret your question as to what, specifically, you want to do. It may be worth having a read of "How to Ask" for additional pointers. – David Thomas Commented Feb 4, 2017 at 11:11 - Gotcha, thanks guys it makes sense, Thanks TJ for pointing out a better method, and thanks David for the CSS tip off – danjbh Commented Feb 4, 2017 at 11:12
2 Answers
Reset to default 3If you mean you want to adjust the appearance of a disabled button, you do that with a CSS attribute presence selector (input[type=button][disabled]
) or a CSS :disabled
pseudo-class selector (input[type=button]:disabled
):
input[type=button][disabled] {
color: red;
opacity: 0.5;
}
/* or: */
input[type=button]:disabled {
color: red;
opacity: 0.5;
}
The attribute selector works because a disabled button has a disabled
attribute, and an enabled one does not. But in modern code, I'd probably use :disabled
instead.
Example:
var btn = $("#toggle");
setInterval(function() {
btn.prop("disabled", !btn.prop("disabled"));
}, 1000);
/* Here I've used each of the two possible options
to provide half of the styling you wanted.
You'd obviously just choose one of them and use
it to handle both parts of the styling.
*/
input[type=button][disabled] {
color: red;
}
input[type=button]:disabled {
opacity: 0.5;
}
<input type="button" value="This is enabled">
<input type="button" disabled value="This is disabled">
<input id="toggle" type="button" value="This one goes back and forth">
<script src="https://ajax.googleapis./ajax/libs/jquery/2.1.1/jquery.min.js"></script>
Here is one way you can add the styling:
<button class="btn-disabled" disabled> Test </button>
button:disabled.btn-disabled {
opacity: 0.5;
}
$("button").addClass( "btn-disabled" );
本文标签: javascriptAdding a class or style to a disabled buttonStack Overflow
版权声明:本文标题:javascript - Adding a class or style to a disabled button - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745164597a2645611.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论