单击整个元素以注册单选按钮值
Clicking on entire element to register radio button value
我希望能够点击整个元素,而不仅仅是单选按钮和文本,以便注册与单选按钮相关的值。
<div class='btn'>
<input id = '4' class='btn4' type="radio" name='1' value='' >
<label for = '4' class="label4">Question populated from jQuery goes here
</label></div>
当我用标签包装输入时,我会丢失jQuery放入元素中的文本。
我必须填充文本的功能是…
function populateQuestion(index) {
if (currentQuestion <=9){
$('.q_question').html(questionsArray[index]['question']);
for (var i = 0; i < 8; i++) {
$('.jumbotron').html('');
$('.btn' + (i + 1)).val(questionsArray[index]['choices'][i]).prop('checked', false);
$('.label' + (i + 1)).html(questionsArray[index]['choices'][i]);
}
} else{ quizComplete(correctAnswers)}
}
populateQuestion(currentQuestion);
有趣的小问题。我试图在jQuery的一行中完成这项工作,但很快意识到,由于正在更改的元素位于容器内,尝试单击它们会重置它们(有效地使它们不可单击)。解决方案是在CSS中的容器顶部添加一个不可见的伪元素,这样你就不能真正点击单选按钮(或标签)。
$('div.btn').on('click', function() {
$(this).children('input').prop('checked', !$(this).children('input').prop('checked'));
})
.btn {
border: 1px solid red;
padding: 12px;
position: relative;
}
.btn::after {
content: "";
position: absolute;
width: 100%;
height: 100%;
top: 0;
left: 0;
right: 0;
bottom: 0;
z-index: 2;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div class='btn'>
<input id='4' class='btn4' type="radio" name='1' value=''>
<label for='4' class="label4">Question populated from jQuery goes here
</label>
</div>
您可以为外部div注册一个点击事件,并执行您想要执行的任何代码。
$(function(){
$("div.btn").click(function(e){
e.preventDefault();
alert("clicked");
var _this=$(this);
var radio = _this.find("input[type='radio']");
//Do something now
alert('id:'+radio.attr("id")); //Id of radio button
alert('value:'+radio.val()); //value of radio button
});
});
这是一个工作样品。
相关文章:
- 多个单选按钮组相互干扰
- 是否可以禁用jquery中的单个单选按钮
- Javascript:无法获取变量中的单选按钮值
- 通过ajax到php文件获取单选按钮值
- 互斥单选按钮和相应的输入字段
- 用于选择MVC3中单选按钮值的JavaScript
- 如何获取单选按钮的多个实例的选定单选按钮值
- react testUtils模拟点击单选按钮而不触发onchange
- 当选择组中的单选按钮时,jQuery addClass
- Angularjs无法将单选按钮与嵌套范围内的模型绑定
- 客户端单选按钮列表验证
- Javascript:图像与单选按钮的值不匹配
- 如何在Javascript中将单选按钮值保存到cookie中
- 单击时如何更改单选按钮的CSS
- 使用按钮和单选按钮提交表单,但始终使用相同的值
- 如何返回选中正确单选按钮的注册页面
- 单击整个元素以注册单选按钮值
- 选中单选按钮,未在change()中注册
- 在joomla 2.5中自定义注册表单-基于单选按钮值动态启用字段
- Jquery没有将我的一个单选按钮注册为选中