首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用jquery在非checkboxradio对象上按值设置单选按钮(按钮在css中的行为不同)

使用jquery在非checkboxradio对象上按值设置单选按钮(按钮在css中的行为不同)
EN

Stack Overflow用户
提问于 2016-08-28 08:31:17
回答 1查看 66关注 0票数 1

正如您在代码段中所看到的,我的单选按钮在css中的行为有所不同。我很难理解是什么问题,以及如何妥善处理这一问题。我不能调用refresh,因为它们没有初始化为jQuerycheckboxradio

如果没有CSS,单选按钮的行为就像预期的那样,但是在CSS中,可能会有更多的知识,但在我的情况下却是多余的。

代码语言:javascript
复制
$("#btn0").click(function(e) {

  document.getElementById("rbtShareExp0").checked = true;

});


$("#btn1").click(function(e) {

  document.getElementById("rbtShareExp1").checked = true;

});

$("#btn0a").click(function(e) {

  document.getElementById("rbtShareExp0_a").checked = true;

});


$("#btn1a").click(function(e) {

  document.getElementById("rbtShareExp1_a").checked = true;

});
代码语言:javascript
复制
.switch-field {
  font-family: Arial;
  overflow: hidden;
}
.switch-title {
  margin-bottom: 6px;
}
.switch-field input {
  display: none;
}
.switch-field label {
  float: left;
}
.switch-field label {
  display: inline-block;
  background-color: #D9EDF7;
  color: #1159a5;
  font-size: 12px;
  font-weight: bold;
  text-align: center;
  padding: 6px 14px;
  border: 1px solid rgba(0, 0, 0, 0.2);
  -webkit-box-shadow: inset 0 1px 3px rgba(0, 0, 0, 0.3), 0 1px rgba(255, 255, 255, 0.1);
  box-shadow: inset 0 1px 3px rgba(0, 0, 0, 0.3), 0 1px rgba(255, 255, 255, 0.1);
  -webkit-transition: all 0.3s ease-in-out;
  -moz-transition: all 0.3s ease-in-out;
  -ms-transition: all 0.3s ease-in-out;
  -o-transition: all 0.3s ease-in-out;
  transition: all 0.3s ease-in-out;
}
.switch-field label:hover {
  cursor: pointer;
}
.switch-field input:checked + label {
  background-color: #A5DC86;
  -webkit-box-shadow: none;
  box-shadow: none;
}
.switch-field label:first-of-type {
  border-radius: 4px 0 0 4px;
}
.switch-field label:last-of-type {
  border-radius: 0 4px 4px 0;
}
代码语言:javascript
复制
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<fieldset>
  <legend>with CSS:</legend>
  <div class="switch-field">
    <label for="rbtShareExp0">No way</label>
    <input type="radio" class="rbtShare" name="rbtShareExp" id="rbtShareExp0" value="0" />
    <label for="rbtShareExp1">Yes, share it</label>
    <input type="radio" class="rbtShare" name="rbtShareExp" id="rbtShareExp1" value="1" />
  </div>

  <input type='button' id='btn0' value='set to 0' />
  <input type='button' id='btn1' value='set to 1' />
</fieldset>


<fieldset>
  <legend>without CSS:</legend>

  <label for="rbtShareExp0">No way</label>
  <input type="radio" class="rbtShare" name="rbtShareExp" id="rbtShareExp0_a" value="0" />
  <label for="rbtShareExp1">Yes, share it</label>
  <input type="radio" class="rbtShare" name="rbtShareExp" id="rbtShareExp1_a" value="1" />
</br>
  <input type='button' id='btn0a' value='set to 0' />
  <input type='button' id='btn1a' value='set to 1' />
</fieldset>

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-08-28 08:44:48

这是因为你的造型规则。样式标签的活动状态取决于.switch-field input:checked + label,这意味着如果您检查第一个收音机,它将激活第二个标签。因此,您应该让标签按单选按钮,就像我在下面所做的那样,它们将正确工作:

代码语言:javascript
复制
$("#btn0").click(function(e) {

  document.getElementById("rbtShareExp0").checked = true;

});


$("#btn1").click(function(e) {

  document.getElementById("rbtShareExp1").checked = true;

});

$("#btn0a").click(function(e) {

  document.getElementById("rbtShareExp0_a").checked = true;

});


$("#btn1a").click(function(e) {

  document.getElementById("rbtShareExp1_a").checked = true;

});
代码语言:javascript
复制
.switch-field {
  font-family: Arial;
  overflow: hidden;
}
.switch-title {
  margin-bottom: 6px;
}
.switch-field input {
  display: none;
}
.switch-field label {
  float: left;
}
.switch-field label {
  display: inline-block;
  background-color: #D9EDF7;
  color: #1159a5;
  font-size: 12px;
  font-weight: bold;
  text-align: center;
  padding: 6px 14px;
  border: 1px solid rgba(0, 0, 0, 0.2);
  -webkit-box-shadow: inset 0 1px 3px rgba(0, 0, 0, 0.3), 0 1px rgba(255, 255, 255, 0.1);
  box-shadow: inset 0 1px 3px rgba(0, 0, 0, 0.3), 0 1px rgba(255, 255, 255, 0.1);
  -webkit-transition: all 0.3s ease-in-out;
  -moz-transition: all 0.3s ease-in-out;
  -ms-transition: all 0.3s ease-in-out;
  -o-transition: all 0.3s ease-in-out;
  transition: all 0.3s ease-in-out;
}
.switch-field label:hover {
  cursor: pointer;
}
.switch-field input:checked + label {
  background-color: #A5DC86;
  -webkit-box-shadow: none;
  box-shadow: none;
}
.switch-field label:first-of-type {
  border-radius: 4px 0 0 4px;
}
.switch-field label:last-of-type {
  border-radius: 0 4px 4px 0;
}
代码语言:javascript
复制
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<fieldset>
  <legend>with CSS:</legend>
  <div class="switch-field">
    <input type="radio" class="rbtShare" name="rbtShareExp" id="rbtShareExp0" value="0" /><label for="rbtShareExp0">No way</label>
    
    <input type="radio" class="rbtShare" name="rbtShareExp" id="rbtShareExp1" value="1" /><label for="rbtShareExp1">Yes, share it</label>
    
  </div>

  <input type='button' id='btn0' value='set to 0' />
  <input type='button' id='btn1' value='set to 1' />
</fieldset>


<fieldset>
  <legend>without CSS:</legend>

  <label for="rbtShareExp0">No way</label>
  <input type="radio" class="rbtShare" name="rbtShareExp" id="rbtShareExp0_a" value="0" />
  <label for="rbtShareExp1">Yes, share it</label>
  <input type="radio" class="rbtShare" name="rbtShareExp" id="rbtShareExp1_a" value="1" />
  </br>
  <input type='button' id='btn0a' value='set to 0' />
  <input type='button' id='btn1a' value='set to 1' />
</fieldset>

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/39189375

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档