我有一个通过jQuery 11.1的单选按钮点击事件的问题。如果我点击div,单选按钮的状态在第一次就会变成点击,但是我一次又一次地点击,而不是它不起作用。这是我的代码HTML代码
<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代码
<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');
}
});
有什么我没注意到的地方吗?
发布于 2016-10-28 18:24:03
您可以尝试:
$(".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?
});.selected {
border: 1px solid #00f
}<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>
发布于 2016-10-28 18:17:17
您在brandSection类中有一个拼写错误
if ($('input[type="radio"]', this).attr('checked', 'checked') == true) {
$('.brandSection').removeClass('selected');
}最好将您的代码包装在文档就绪语句中
$(function(){
//your code here
})使用find()选择相对于当前单击的元素的输入
发布于 2016-10-28 18:34:34
我建议,基于对代码中希望发生的事情的一些小猜测:
// 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();.selected {
border: 1px solid red;
}<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>
原始代码中的错误是下面这行:
$('input[type="radio"]', this).attr('checked', 'checked');不幸的是,checked属性不会更新checked属性。
参考文献:
:
:
click().:
https://stackoverflow.com/questions/40302851
复制相似问题