最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Apply CSS to disabled input button - Stack Overflow

programmeradmin1浏览0评论
    <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"));
         });

This code works fine, only enabling the button if a check box is clicked. However I want to use the css classes 'class="edit red btn"', and although the functionality is still working the button appears visible.

I would like to add one css class if button is enabled and another if it is disabled...How Can I do this? thanks

    <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"));
         });

This code works fine, only enabling the button if a check box is clicked. However I want to use the css classes 'class="edit red btn"', and although the functionality is still working the button appears visible.

I would like to add one css class if button is enabled and another if it is disabled...How Can I do this? thanks

Share Improve this question asked Mar 31, 2015 at 10:05 JohnJohn 1031 gold badge4 silver badges11 bronze badges
Add a ment  | 

5 Answers 5

Reset to default 3

You can add a new css class here:

submitButtShare.attr("disabled", !checkboxes.is(":checked")).toggleClass('disabled');

now in the css you can use this:

#buttonShareMap.disabled{
    background:#c5c5c5;
    color:#ddd;
}

checkout the sample demo:

$(':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./ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type='button' id='buttonShareMap' class='red' value='buttonShareMap' />
<input type='checkbox' />

you can set background color for disable elements, by using below code.

#buttonShareMap:disabled {
    background: #f00;
}

Just check the button disabled attr or just the checkbox checked value and set classes accordingly. Below I have used the checkbox state.

     checkboxes.click(function () {
         submitButtShare.attr("disabled", !checkboxes.is(":checked"));
         if(checkboxes.is(":checked"))
         {
              submitButtShare.addClass('ifButtonEnabled')
              .removeClass('ifButtoDisabled');

         }
         else
         {
             submitButtShare.addClass('ifButtoDisabled')
             .removeClass('ifButtonEnabled');

         }
     });

Helo,

I have made an example for you: http://jsfiddle/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;
}

What I am doing is check everytime the user change the checkbox and then I add a class.

I hope it's helps!

Here is the HTML

<input disabled="disabled" class="btn btn-blue span3" type="submit" value="Change">

Here is the CSS

input[disabled].btn:hover,input[disabled].btn:hover,input[disabled].btn:focus{
color:yellow}
发布评论

评论列表(0)

  1. 暂无评论