它不工作,但如果我註釋掉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');
})
你不需要**從另一個元素中刪除'checked'屬性。它們是具有相同名稱的無線電輸入 - 無論如何,瀏覽器都會處理相互排斥。 – 2012-01-18 14:06:55