[英]Check Radio button on div click
I would like radio button to check/uncheck whenever i click it's parent div.我希望单选按钮在我单击它的父 div 时选中/取消选中。 Radio buttons are originally hidden.单选按钮最初是隐藏的。
I was trying to achieve that by wrapping <label>
around the div.我试图通过将<label>
包裹在 div 周围来实现这一点。 It works, but jQ.toggleClass
stops working.它有效,但jQ.toggleClass
停止工作。
HTML HTML
<div class="big">
This is a div 1
<input id="chb" type="radio" />
</div>
<br/>
<div class="big">
This is a div 2
<input id="chb" type="radio" />
</div>
CSS CSS
.big {
width:100px;
height:100px;
background-color:red;
cursor:pointer;
}
.hli {
border:2px solid blue;
}
/*.chb{display:none;}*/
JQ JQ
$('.big').click(function() {
$('.hli').toggleClass('hli');
$(this).toggleClass('hli');
});
JSFIDDLE: http://jsfiddle.net/QqVCu/2/ JSFIDDLE:http: //jsfiddle.net/QqVCu/2/
How about that: http://jsfiddle.net/sgospodarets/QqVCu/5/ ?那怎么样:http: //jsfiddle.net/sgospodarets/QqVCu/5/ ? All that is necessary - wrap inputs in label.所有必要的 - 将输入包装在标签中。 Then do not need JavaScipt.那么就不需要JavaScipt了。
Using a pure HTML/CSS solution:使用纯 HTML/CSS 解决方案:
.isHidden { display: none; /* hide radio buttons */ }.label { display: inline-block; background-color: red; width: 120px; height: 120px; padding: 5px 10px; }.radio:checked +.label { /* target next sibling (+) label */ background-color: blue; }
<input id="radio_1" class="radio isHidden" name="radio_a" type="radio"> <label for="radio_1" class="label">1</label> <input id="radio_2" class="radio isHidden" name="radio_a" type="radio"> <label for="radio_2" class="label">2</label>
Tell me that if u want this: http://jsfiddle.net/QqVCu/6/告诉我,如果你想要这个:http: //jsfiddle.net/QqVCu/6/
jQuery (Updated) jQuery (更新)
$('.big').click(function() {
if($(this).find('input[type="radio"]').is(':checked')){
$(this).find('input[type="radio"]').prop('checked', false);
}
else{
$(this).find('input[type="radio"]').prop('checked', true);
}
$('.hli').toggleClass('hli');
$(this).toggleClass('hli');
});
you can use prop()
method, try the following:您可以使用prop()
方法,尝试以下操作:
$('.big').click(function() {
$('.hli').removeClass('hli');
$(this).addClass('hli').find('input').prop('checked', true)
});
please note that IDs must be unique and in order to work properly radio buttons should have name properties:请注意,ID 必须是唯一的,为了正常工作,单选按钮应该具有名称属性:
<input id="chb1" type="radio" name="radioGroup"/>
<input id="chb2" type="radio" name="radioGroup"/>
Try this: http://jsfiddle.net/ZuXvM/试试这个:http: //jsfiddle.net/ZuXvM/
To have a single radio button checked, you should group them by name (give same name for all the buttons that are part of the group)要选中单个单选按钮,您应该按名称对它们进行分组(为属于该组的所有按钮指定相同的名称)
Try this:试试这个:
http://jsfiddle.net/QqVCu/50/ http://jsfiddle.net/QqVCu/50/
To bypass jQuery event bubbling I've used regular JS to trigger the click().为了绕过 jQuery 事件冒泡,我使用了常规 JS 来触发 click()。
$('.big').click(function () {
$('.hli').toggleClass('hli');
$(this).toggleClass('hli');
var Id = $(this).find('input[type=radio]').attr('id');
document.getElementById(Id).click();
});
If you want to use JQuery, this is how you should do it如果你想使用 JQuery,这就是你应该做的
$('.big').on({
click: function () {
$('.hli').toggleClass('hli');
$(this).toggleClass('hli');
}
}, 'input[name="chb"]');
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.