我有以下方式绑定到复选框的无线电按钮列表。
1)选中复选框时,启用了无线电按钮并检查了一个默认按钮(例如,值= 1)(从三个可以选择一个单选按钮)
2)当不选中复选框时,无线电按钮将被禁用并删除其选择属性。
我面临的问题是,由于以下html和jQuery代码,页面加载默认的单选按钮(value = 1)无法正常工作。我无法识别错误。有人可以帮助吗?
$('#mailcheck').change(function() {
$('input[name="freq"]').prop('disabled', !this.checked);
$('input[name="freq"]').removeAttr('checked');
}).change();
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<label id="notcheck">
<input type="checkbox" id="mailcheck" checked="checked">I want to receive an email notification of ads uploaded on my college marketplace
</label>
<br>
<ul>
<li>
<label>
<input type="radio" name="freq" value="1" id="freqradio" checked="checked">Daily</label>
</li>
<li>
<label>
<input type="radio" name="freq" value="3" id="freqradio">Every 3 days</label>
</li>
<li>
<label>
<input type="radio" name="freq" value="7" id="freqradio">Weekly</label>
</li>
</ul>
这是一个粗糙的工作版本:
$('#mailcheck').change(function(){
if($(this).is(':checked')) {
$('input[name="freq"]').prop('disabled', false).first().prop('checked', true);
} else {
$('input[name="freq"]').prop('disabled', true).prop('checked', false);
}
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<label id="notcheck">
<input type="checkbox" id="mailcheck" checked="checked" >I want to receive an email notification of ads uploaded on my college marketplace
</label><br>
<ul>
<li><label><input type="radio" name="freq" value="1" id="freqradio1" checked="checked">Daily</label></li>
<li><label><input type="radio" name="freq" value="3" id="freqradio2">Every 3 days</label></li>
<li><label><input type="radio" name="freq" value="7" id="freqradio3">Weekly</label></li>
</ul>
P.S。IDS必须具有唯一的值