将CSS应用于禁用的输入按钮

时间:2021-06-24 20:11:52
    <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.

此代码工作正常,只有在单击复选框时才启用该按钮。但是我想使用css类'class =“edit red btn”',虽然功能仍在工作,但按钮显示为可见。

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

我想添加一个css类,如果按钮被启用,另一个如果它被禁用...我怎么能这样做?谢谢

5 个解决方案

#1


3  

You can add a new css class here:

您可以在此处添加新的css类:

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

now in the css you can use this:

现在在css中你可以使用这个:

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

#2


1  

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

您可以使用下面的代码设置禁用元素的背景颜色。

#buttonShareMap:disabled {
    background: #f00;
}

#3


1  

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

只需选中按钮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');

         }
     });

#4


1  

Helo,

I have made an example for you: http://jsfiddle.net/o82tfxrb/1/

我为你做了一个例子: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;
}

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

我正在做的是每次用户更改复选框时检查,然后我添加一个类。

I hope it's helps!

我希望它有所帮助!

#5


1  

Here is the HTML

这是HTML

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

Here is the CSS

这是CSS

input[disabled].btn:hover,input[disabled].btn:hover,input[disabled].btn:focus{
color:yellow}

#1


3  

You can add a new css class here:

您可以在此处添加新的css类:

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

now in the css you can use this:

现在在css中你可以使用这个:

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

#2


1  

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

您可以使用下面的代码设置禁用元素的背景颜色。

#buttonShareMap:disabled {
    background: #f00;
}

#3


1  

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

只需选中按钮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');

         }
     });

#4


1  

Helo,

I have made an example for you: http://jsfiddle.net/o82tfxrb/1/

我为你做了一个例子: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;
}

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

我正在做的是每次用户更改复选框时检查,然后我添加一个类。

I hope it's helps!

我希望它有所帮助!

#5


1  

Here is the HTML

这是HTML

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

Here is the CSS

这是CSS

input[disabled].btn:hover,input[disabled].btn:hover,input[disabled].btn:focus{
color:yellow}