我遇到了一个我不确定如何解决的问题。根据我使用firefox和简单控制台日志进行的调试,我这样做是正确的。当我选择每个按钮时,我可以看到为选中的单选按钮切换指定的div id。但是,当使用addClass()函数时,对于选中的单选按钮,指定的类不会应用于所选的div id。哇,好大的一口。
<script type="text/javascript">
var $j = jQuery.noConflict();
$j(document).ready(function(){
$j('#div :radio').buttonset().click(function(){
$j(this).find(':checked').attr('id').addClass('');
$j(this).find(':checked').attr('id').addClass('checked');
});
});
</script>
<style>
.checked{background:url(icon.png) left no-repeat;}
</style>
<div id="div">
<p><input type="radio" id="radio-1" name="radio" value="radio-1" /><label for="radio-1">Radio-1</label></p>
<p><input type="radio" id="radio-2" name="radio" value="radio-2" /><label for="radio-2">Radio-2</label></p>
<p><input type="radio" id="radio-3" name="radio" value="radio-3" /><label for="radio-3">Radio-3</label></p>
</div>发布于 2011-08-19 10:59:54
.attr()返回一个字符串,您的代码对该字符串调用.addClass()。很明显,这是行不通的。您不希望将类添加到ID中--事实上,您不能这样做,因为没有String.addClass方法。
试试这个:
$j('#div').delegate('input[type="radio"]', 'change', function ()
{
$j(this).parent().toggleClass('checked', this.checked);
});编辑
这更像是你要找的东西吗?http://jsfiddle.net/mattball/ACREd/
https://stackoverflow.com/questions/7116472
复制相似问题