<input type="button" value="Share" id="buttonShareMap" style="float:right; width:68px;" disabled="disabled"/>
/* Only enable 'SHARE' btns if a checkbox is selected */
var checkboxes = $("input[type='checkbox']"),
submitButtShare = $("#buttonShareMap");
checkboxes.click(function () {
submitButtShare.attr("disabled", !checkboxes.is(":checked"));
});
此代码工作正常,只有在单击复选框时才启用该按钮。但是我想使用css类'class =“edit red btn”',虽然功能仍在工作,但按钮显示为可见。
如果按钮已启用,我想添加一个css类,如果禁用该按钮,我想添加另一个...我怎么能这样做?感谢
答案 0 :(得分:3)
您可以在此处添加新的css类:
submitButtShare.attr("disabled", !checkboxes.is(":checked")).toggleClass('disabled');
现在在css中你可以使用它:
#buttonShareMap.disabled{
background:#c5c5c5;
color:#ddd;
}
结帐示例演示:
$(':checkbox').change(function() {
$('#buttonShareMap').prop('disabled', this.checked).toggleClass('disabled');
});
.red {
color: red;
background:#0ff;
}
#buttonShareMap.disabled {
background: #c5c5c5;
color: #ddd;
cursor: not-allowed;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type='button' id='buttonShareMap' class='red' value='buttonShareMap' />
<input type='checkbox' />
答案 1 :(得分:1)
只需选中按钮attr或仅选中复选框选中的值并相应地设置类。下面我使用了复选框状态。
checkboxes.click(function () {
submitButtShare.attr("disabled", !checkboxes.is(":checked"));
if(checkboxes.is(":checked"))
{
submitButtShare.addClass('ifButtonEnabled')
.removeClass('ifButtoDisabled');
}
else
{
submitButtShare.addClass('ifButtoDisabled')
.removeClass('ifButtonEnabled');
}
});
答案 2 :(得分:1)
您可以使用以下代码为禁用元素设置背景颜色。
#buttonShareMap:disabled {
background: #f00;
}
答案 3 :(得分:1)
Helo,
我为你做了一个例子:http://jsfiddle.net/o82tfxrb/1/
JS:
$(document).ready(function(){
$('#ck').on('change', function(){
if ($(this).is(':checked')){
$('#buttonShareMap').removeClass('red')
.addClass('blue');
}else{
$('#buttonShareMap').removeClass('blue')
.addClass('red');
}
});
});
HTML:
<input type="checkbox" id="ck" />
<input type="button" class="red" value="Share" id="buttonShareMap"/>
的CSS:
.red{
background-color:red;
}
.blue{
background-color:blue;
color: white;
}
我正在做的是每次用户更改复选框时检查,然后我添加一个类。
我希望它有所帮助!
答案 4 :(得分:1)
这是HTML
<input disabled="disabled" class="btn btn-blue span3" type="submit" value="Change">
这是CSS
input[disabled].btn:hover,input[disabled].btn:hover,input[disabled].btn:focus{
color:yellow}