javascript - jQuery 禁用启用按钮样式

标签 javascript jquery css

我正在尝试使用 jquery 制作禁用启用按钮样式。

这是我的 DEMO来自 Codepen 的页面

在我的演示中,您可以看到有一个蓝色的提交按钮。当您在输入字段中输入内容时,按钮处于事件状态。

我想在这个按钮禁用颜色为红色时添加。如果按钮不可禁用,则按钮颜色变为蓝色。

我已经为按钮创建了 .enableOnInput.red Css 样式。

.enableOnInput {
  width: 200px;
    height: 25px;
    padding: 0;
    -webkit-box-sizing: content-box;
    -moz-box-sizing: content-box;
    box-sizing: content-box;
    color: rgba(255,255,255,1);
    font: bold 10px/25px "Lucida Grande";
    border: 1px solid rgba(0,153,224,1);
    -webkit-border-top-left-radius: 3px;
    -webkit-border-top-right-radius: 3px;
    -webkit-border-bottom-right-radius: 3px;
    -webkit-border-bottom-left-radius: 3px;
    -moz-border-radius-topleft: 3px;
    -moz-border-radius-topright: 3px;
    -moz-border-radius-bottomright: 3px;
    -moz-border-radius-bottomleft: 3px;
    border-top-left-radius: 3px;
    border-top-right-radius: 3px;
    border-bottom-right-radius: 3px;
    border-bottom-left-radius: 3px;
    background-image: rgba(66,66,66,1);
    background-image: -webkit-linear-gradient(top, #3db5ed 0%,#0099e0 100%);
    background-image: -moz-linear-gradient(top, #3db5ed 0%,#0099e0 100%);
    background-image: -o-linear-gradient(top, #3db5ed 0%,#0099e0 100%);
    background-image: -ms-linear-gradient(top, #3db5ed 0%,#0099e0 100%);
    background-image: linear-gradient(top, #3db5ed 0%,#0099e0 100%);
}
.red {
    width: 20px;
    height: 25px;
    padding: 0;
    -webkit-box-sizing: content-box;
    -moz-box-sizing: content-box;
    box-sizing: content-box;
    color: rgba(255,255,255,1);
    font: bold 10px/25px "Lucida Grande";
    border: 1px solid rgba(0,153,224,1);
    -webkit-border-top-left-radius: 3px;
    -webkit-border-top-right-radius: 3px;
    -webkit-border-bottom-right-radius: 3px;
    -webkit-border-bottom-left-radius: 3px;
    -moz-border-radius-topleft: 3px;
    -moz-border-radius-topright: 3px;
    -moz-border-radius-bottomright: 3px;
    -moz-border-radius-bottomleft: 3px;
    border-top-left-radius: 3px;
    border-top-right-radius: 3px;
    border-bottom-right-radius: 3px;
    border-bottom-left-radius: 3px;
    background-image: rgba(66,66,66,1);
    background-image: -webkit-linear-gradient(top, #ff0000 0%,#c20202 100%);
    background-image: -moz-linear-gradient(top, #ff0000 0%,#c20202 100%);
    background-image: -o-linear-gradient(top, #ff0000 0%,#c20202 100%);
    background-image: -ms-linear-gradient(top, #ff0000 0%,#c20202 100%);
    background-image: linear-gradient(top, #ff0000 0%,#c20202 100%);
}

这是我用于禁用启用的 jquery:

$(function(){
     $('#searchInput, #searchInput2').keyup(function(){
          if ($(this).val() == '') { //Check to see if there is any text entered
               //If there is no text within the input ten disable the button
               $('.enableOnInput').attr('disabled', 'true');
               $(".aa").show();
               $(".bb").hide();
          } else {
               //If there is text in the input, then enable the button
               $('.enableOnInput').attr('disabled', false);
               $(".aa").hide();
               $(".bb").show();
          }
     });
});

最佳答案

添加这个CSS规则:

input:disabled
{
  background-color: red;
  /*other style properties*/
}

关于javascript - jQuery 禁用启用按钮样式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26252488/

相关文章:

javascript - 如何隐藏附加到 div 的图像

javascript - 全局 Javascript var 变为 null

javascript - AJAX 从 JSON 树获取

Javascript 的 "document.write()"中的 PHP 代码

jquery - 更新后如何滚动到 JSF 组件的底部?

javascript - 如何使用 iOS 版 Phonegap 获取当前时间?

javascript - 在悬停一个元素时切换 div,然后关闭另一个元素?

css - 免费 wordpress.com 网站上的全 Angular 页面

javascript - 如何使用 pygtk+ WebKit 自动填充输入字段

javascript - 不使用网格的贪吃蛇游戏算法,段的移动速度可能比它们的尺寸慢