【问题标题】:Changing radio button background on selected according to it value根据其值更改选定的单选按钮背景
【发布时间】:2017-08-16 03:52:41
【问题描述】:

目标

尝试更改样式为按钮的单选输入的背景颜色,以便当用户单击它时,颜色将根据单选值更改。

预期结果

当点击“NEUTRAL”按钮时,将按钮背景颜色更改为白色。
单击“GOOD”按钮时,将按钮背景颜色更改为绿色。
单击“WATCHLIST”按钮时,将按钮背景更改为黄色。
当单击“UNDER MONOTORING”按钮时,将按钮背景更改为红色。

问题

它不会相应地改变按钮的背景,这是我想要做的。

在这里感谢一些帮助。提前致谢。

$(document).ready(function() {
  $('label').click(function() {
    if ($("input:radio[name='category']:checked").val() == 'W') {
      $(this).addClass('whiteBG');
    }
    if ($("input:radio[name='category']:checked").val() == 'G') {
      $(this).addClass('greenBG');
    }
    if ($("input:radio[name='category']:checked").val() == 'Y') {
      $(this).addClass('yellowBG');
    }
    if ($("input:radio[name='category']:checked").val() == 'R') {
      $(this).addClass('redBG');
    }
  });
});
input[type=radio],
input[type=checkbox] {
  display: none;
}

label {
  display: block;
  appearance: button;
  -webkit-appearance: button;
  -moz-appearance: button;
  -ms-appearance: button;
  font-family: 'Roboto', sans-serif;
  font-weight: 400;
  background: #DDDDDD;
  font-size: 1.6rem;
  color: #111111;
  border: 2px solid #AAAAAA;
  padding: 8px;
  width: 40%;
  margin: 0 auto;
  text-align: center;
  transition: all 0.7s ease-in-out;
}

.whiteBG {
  background-color: #FFF000;
}

.greenBG {
  background-color: #0F0000;
}

.yellowBG {
  background-color: #FF0000;
}

.redBG {
  background-color: #F00000;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<form>
  <fieldset data-role="controlgroup">
    <legend>PERSON CATEGORY SELECTION</legend>
    <label for="neutral">
              <input type="radio" class="button" id="neutral" name="category" value="W" >NEUTRAL</input>
          </label>
    <label for="good">
              <input type="radio" class="button" id="good" name="category" value="G">GOOD</input>
          </label>
    <label for="watchlist">
              <input type="radio" class="button" id="watchlist" name="category" value="Y">WATCHLIST</input>
          </label>
    <label for="monitor">
              <input type="radio" class="button" id="monitor	" name="category" value="R">UNDER MONOTORING</input>
          </label>
  </fieldset>
</form>

【问题讨论】:

  • 您是说单击其中一个按钮应该将那个按钮的背景颜色更改为指定颜色,并将其他按钮更改回灰色?
  • 是的,其他的应该变回灰色。

标签: javascript jquery html css


【解决方案1】:

代码中的问题:

  • CSS 中的# 颜色代码没有反映您尝试分配的颜色。这些十六进制代码需要是缩写的三位 RGB 值或六位 RGB 值,每种颜色有两位,即#RGB#RRGGBB,但就像您通过添加“000”来混合这两个选项到你想要的颜色的正确三位十六进制代码的末尾。删除每一个末尾的 000,或更改为正确的六位十六进制代码。
  • 当单击其他按钮时,您没有任何代码可以从按钮中删除该类。

您的 JS 代码似乎过于复杂。我会将点击处理程序绑定到单选按钮本身,因为 this.value 会给您刚刚单击的按钮的值,从而大大简化了您的 if 条件。您可以使用$(this).parent() 来获取标签元素以设置样式。

我引入了一个名为 buttons 的变量,它是包含所有按钮的 jQuery 对象,因为在处理程序中你可以说 buttons.not(this).parent() 来获取一个包含所有其他按钮的父标签元素的 jQuery 对象并从中删除颜色类以使它们再次变灰。

$(document).ready(function() {
  var buttons = $("input:radio[name='category']").click(function() {
    buttons.not(this).parent().removeClass('whiteBG greenBG yellowBG redBG');
    var label = $(this).parent();
    if (this.value == 'W') {
      label.addClass('whiteBG');
    } else if (this.value == 'G') {
      label.addClass('greenBG');
    } else if (this.value == 'Y') {
      label.addClass('yellowBG');
    } else if (this.value == 'R') {
      label.addClass('redBG');
    }
  });
});
input[type=radio], input[type=checkbox] { display: none; }

label {
  display: block;
  appearance: button; -webkit-appearance: button; -moz-appearance: button; -ms-appearance: button;
  font-family: 'Roboto', sans-serif; font-weight: 400;
  background: #DDDDDD;
  font-size: 1.6rem;
  color: #111111;
  border: 2px solid #AAAAAA;
  padding: 8px;
  width: 40%;
  margin: 0 auto;
  text-align: center;
  transition: all 0.7s ease-in-out;
}

.whiteBG { background-color: #FFF; }

.greenBG { background-color: #0F0; }

.yellowBG { background-color: #FF0; }

.redBG { background-color: #F00; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<form>
  <fieldset data-role="controlgroup">
    <legend>PERSON CATEGORY SELECTION</legend>
    <label for="neutral">
        <input type="radio" class="button" id="neutral" name="category" value="W" >NEUTRAL</input>
    </label>
    <label for="good">
         <input type="radio" class="button" id="good" name="category" value="G">GOOD</input>
    </label>
    <label for="watchlist">
         <input type="radio" class="button" id="watchlist" name="category" value="Y">WATCHLIST</input>
    </label>
    <label for="monitor">
         <input type="radio" class="button" id="monitor" name="category" value="R">UNDER MONOTORING</input>
    </label>
  </fieldset>
</form>

【讨论】:

  • 如果我有一个从 db 读取的类别值让我们说“R”,如何设置按钮检查或在页面加载时选择?
  • 您可以通过在选择器中使用其 value 属性来触发该特定按钮上的单击事件:假设所需的“R”值在变量 savedVal 中,然后类似于 $("input:radio[name='category'][value='" + savedVal + "']").click() (添加在里面您现有的文档在其他代码之后准备就绪)。
  • 我的意思是我有一个来自其他页面的传递值“R”,我希望选择“正在监控”按钮并将背景设置为红色作为默认值。
  • 是的,我明白了。因此,首先从请求参数中获取值(如果您无法弄清楚如何做到这一点,请专门提出另一个问题,提及您正在使用的服务器端技术),一旦您在变量中获得值,请尝试就像我在之前的评论中展示的那样。
  • 好的,我添加如下:
【解决方案2】:

为了让您的background-color 出现,您应该将label 上的appearance 属性设置为none 而不是button

label {
  appearance: none;
  -webkit-appearance: none;
  -moz-appearance: none;
  -ms-appearance: none;
}

在设置新的 CSS 类之前,您应该在点击时从 button 中删除所有 CSS 类。

【讨论】:

  • “为了让你的背景颜色显示出来” - 在 OP 的 sn-p 中,点击时已经显示了不同的背景颜色,而不是预期的颜色。将 appearance 更改为 none 对颜色没有帮助,而且 OP 显然希望标签看起来像按钮。当我显示的代码按原样运行时,我不确定您为什么尝试将该更改编辑为 my 答案。
  • @nnnnnn 我不知道您使用的是哪个浏览器,但是在 OP 和您的 sn-p 中,Chrome 中都没有显示颜色。这就是我编辑你的答案的原因。我认为看不到颜色也是个大问题。
  • 我只在 Chrome 中测试过。对我来说效果很好。
  • 奇怪,OP 中的 sn-p 在 Mac Chrome 60.0.3112.101 中对我不起作用。
猜你喜欢
  • 2018-03-24
  • 2018-10-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-10-03
  • 1970-01-01
相关资源
最近更新 更多