它不起作用,但如果我评论uncheck.removeAttr('checked')
,它就可以了! 我哪里错了?
小HTML例如:
<input type="radio" name="type" value="duration" checked="true" />
<select id="duration" class="duration">
<option value="P30D">30 giorni</option>
<option value="P1Y">365 giorni</option>
</select>
<input type="radio" name="type" value="interval" />
<select id="year">
<?php foreach($years as $y) : ?>
<option value="<?php echo $y->k; ?>">
<?php echo $y->v; ?>
</option>
<?php endforeach; ?>
</select>
jQuery click()
函数事件:
$('select').click(function() {
var check = $('input[type="radio"][value="duration"]');
var uncheck = $('input[type="radio"][value="interval"]');
if(!$(this).hasClass('duration')) {
var old = check;
check = uncheck;
uncheck = old;
}
check.attr('checked', true); // Same: .attr('checked', 'checked')
uncheck.removeAttr('checked');
})
答案 0 :(得分:1)
应该是
check.attr('checked', 'checked');
<强>更新强>
好的回答你的问题,如何从选择框中选择一个值时选择单选按钮,你会得到以下javascript:
$('select').click(function() {
$('input[type="radio"]').removeAttr('checked'); // remove all checked from radio inputs
$(this).prev('input[type="radio"]').attr('checked', 'checked'); // checks the above radio button
});
答案 1 :(得分:0)