如您在代码段中所见,我的单选按钮与CSS的行为有所不同。我很难理解问题出在哪里以及如何正确处理。
我无法调用refresh,因为它们没有初始化为jqu​​ery checkboxradio

如果没有CSS,单选按钮的行为将与预期的一样,但是如果您拥有CSS,那么,如果您有更多的知识,可能会期望它们,但就我而言,这是不需要的。



$("#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;

});

.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;
}

<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>

最佳答案

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



$("#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;

});

.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;
}

<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>

10-05 20:40
查看更多