我的表单包含2个带单选按钮的测验问题。
例如:
问题1:美国的资本是多少? 1)纽约 2)DC 3)洛杉矶 4)SF
问题2:谁是现任美国总统? 1)布什 2)克林顿 3)华盛顿 4)奥巴马
我启用了一个javascript,可以阻止用户在选择答案后更改其选择。例如,如果我为问题1选择LA,它将被锁定,我无法更改为SF。
问题是javascript导致问题2的行为相同,所以我无法选择问题2中的任何答案。
这是我正在使用的脚本:
// prevent the user from making another selection once one radio option has been selected
$('input[type=radio]').click(function(){ $('input[type=radio]').prop('disabled',true); });
如何设置此脚本以应用每个问题,而不是所有未回答问题的单选按钮?
答案 0 :(得分:3)
从当前单击的广播获取name
值,然后禁用该特定组:
$('input[type=radio]').click(function() {
var name = $(this).attr("name");
$("input[name=" + name + "]").prop("disabled", true);
});
答案 1 :(得分:2)
问题的所有单选按钮应具有相同的名称。因此,请将名称添加到选择器中。
$('input[type=radio]').click(function(){ $('input[type=radio][name='+$(this).attr('name')+']').prop('disabled',true); });
答案 2 :(得分:2)
所以,假设你有一个这样的表格:
<form name='questions'>
<input name='q1' type='radio' value='a' >a<br />
<input name='q1' type='radio' value='b' >b<br />
<input name='q1' type='radio' value='c' >c<br />
<br />
<input name='q2' type='radio' value='1' >1<br />
<input name='q2' type='radio' value='2' >2<br />
<input name='q2' type='radio' value='3' >3<br />
</form>
然后使用此脚本实现您的目标:
$('input[type=radio]').change(function () {
$('input[name='+this.name+']').prop('disabled', true);
});
答案 3 :(得分:0)
将您的jQuery更改为:
$('input[type=radio]').click(function(){
$('input[name="'+$(this).attr('name')+'"]').prop('disabled',true);
});
在点击功能中使用$('input[type=radio]')
会引用您想要使用$(this)
来引用所点击的所有单选按钮。具体来说,$(this).attr('name')
可以抓取同一组中的单选按钮。