首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >jQuery单选按钮单击

jQuery单选按钮单击
EN

Stack Overflow用户
提问于 2016-10-28 18:10:30
回答 6查看 4K关注 0票数 1

我有一个通过jQuery 11.1的单选按钮点击事件的问题。如果我点击div,单选按钮的状态在第一次就会变成点击,但是我一次又一次地点击,而不是它不起作用。这是我的代码HTML代码

代码语言:javascript
运行
复制
<section class="selection-box">

<div class="row">
  <div class="col-lg-12">
    <div class="selection-box-title">Brand</div>
    <div class="radioStyle clearfix selected brandSection">
      <input name="brandRadio" id="brandRadio" type="radio">
      <label class="selection-box-label col-lg-12">
        <span class="col-lg-6">Desktop </span>
        <span class="col-lg-6 text-right">From $500 </span>
      </label>
    </div>
    <div class="radioStyle clearfix brandSection">
      <input name="brandRadio" id="brandRadio" type="radio">
      <label class="selection-box-label col-lg-12">
        <span class="col-lg-6">Mac </span>
        <span class="col-lg-6 text-right">From $500 </span>
      </label>
    </div>


  </div>
</div>

下面是jQuery代码

代码语言:javascript
运行
复制
<script>
$(".brandSection").click(function () {

  $('input[type="radio"]', this).attr('checked', 'checked');

  if ($('input[type="radio"]', this).attr('checked', 'checked') == true) {
    $('brandSection').removeClass('selected');

  }
  else {
    $('.brandSection').removeClass('selected');
    $(this).addClass('selected');

  }


});

有什么我没注意到的地方吗?

EN

回答 6

Stack Overflow用户

发布于 2016-10-28 18:24:03

您可以尝试:

代码语言:javascript
运行
复制
$(".brandSection").click(function () {
    $(this).find('input[type=radio]').prop('checked', true);

    $('.brandSection').removeClass('selected'); // duplicate lines
    $(this).addClass('selected'); // not understanding for this line
    // why you add class selected when the radio button is NOT clicked?
     
});
代码语言:javascript
运行
复制
.selected {
    border: 1px solid #00f
}
代码语言:javascript
运行
复制
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<section class="selection-box">

<div class="row">
  <div class="col-lg-12">
    <div class="selection-box-title">Brand</div>
    <div class="radioStyle clearfix selected brandSection">
      <input name="brandRadio" id="brandRadio1" type="radio">
      <label class="selection-box-label col-lg-12">
        <span class="col-lg-6">Desktop </span>
        <span class="col-lg-6 text-right">From $500 </span>
      </label>
    </div>
    <div class="radioStyle clearfix brandSection">
      <input name="brandRadio" id="brandRadio2" type="radio">
      <label class="selection-box-label col-lg-12">
        <span class="col-lg-6">Mac </span>
        <span class="col-lg-6 text-right">From $500 </span>
      </label>
    </div>


  </div>
</div>

票数 3
EN

Stack Overflow用户

发布于 2016-10-28 18:17:17

您在brandSection类中有一个拼写错误

代码语言:javascript
运行
复制
if ($('input[type="radio"]', this).attr('checked', 'checked') == true) {
    $('.brandSection').removeClass('selected');

  }

最好将您的代码包装在文档就绪语句中

代码语言:javascript
运行
复制
$(function(){
//your code here
})

使用find()选择相对于当前单击的元素的输入

票数 2
EN

Stack Overflow用户

发布于 2016-10-28 18:34:34

我建议,基于对代码中希望发生的事情的一些小猜测:

代码语言:javascript
运行
复制
// binding the anonymous function of the click() method
// as the event-handler for the click event:
$(".brandSection").click(function() {

  // finding the descendant radio input (using
  // plain JavaScript in this case since it's
  // faster/cheaper than calling jQuery:
  var radio = this.querySelector('input[type=radio]');

  // setting the checked property to (Boolean) true:
  radio.checked = true;

  // adding the 'selected' class-name to the clicked
  // .brandSelection element:
  $(this).addClass('selected')
    // selecting the siblings of that element:
    .siblings()
    // and removing the 'selected' class from those elements
    // (to ensure only one element is selected):
    .removeClass('selected');

// filtering the collection of '.brandSection' elements to
// find the one - if any - with the 'selected' class-name:
}).filter('.selected')
// triggering the 'click' event on that element, in order
// that the appropriate radio-input shows up as selected
// on page-load:
.click();
代码语言:javascript
运行
复制
.selected {
  border: 1px solid red;
}
代码语言:javascript
运行
复制
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<section class="selection-box">
  <div class="row">
    <div class="col-lg-12">
      <div class="selection-box-title">Brand</div>
      <div class="radioStyle clearfix selected brandSection">
        <input name="brandRadio" id="brandRadio" type="radio" />
        <label class="selection-box-label col-lg-12">
          <span class="col-lg-6">Desktop </span>
          <span class="col-lg-6 text-right">From $500 </span>
        </label>
      </div>
      <div class="radioStyle clearfix brandSection">
        <input name="brandRadio" id="brandRadio" type="radio" />
        <label class="selection-box-label col-lg-12">
          <span class="col-lg-6">Mac</span>
          <span class="col-lg-6 text-right">From $500 </span>
        </label>
      </div>


    </div>
  </div>
</section>

JS Fiddle demo

原始代码中的错误是下面这行:

代码语言:javascript
运行
复制
$('input[type="radio"]', this).attr('checked', 'checked');

不幸的是,checked属性不会更新checked属性。

参考文献:

  • CSS

  • JavaScript

  • click().

  • jQuery

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

https://stackoverflow.com/questions/40302851

复制
相关文章

相似问题

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