更改单选按钮图像,帮助

大家好。 我不是一个程序员,但仍然需要做一些编码。

现在我有一个问题,用jQuery更改单选按钮图像。 我有这个网络代码,但它仍然无法正常工作。 它会改变单选按钮图像,但是当我尝试选择时,它只会改变第一个单选框的图像。(每次)

这是我的HTML代码:

asdg asdg

这是css。 我希望我做对了。

 #testcontent .label_check, #testcontent .label_radio { padding-left: 34px; } #testcontent .label_radio { background: url(radio-off.png) no-repeat; } #testcontent .label_check { background: url(check-off.png) no-repeat; } #testcontent label.c_on { background: url(check-on.png) no-repeat; } #testcontent label.r_on { background: url(radio-on.png) no-repeat; } #testcontent .label_check input, #testcontent .label_radio input { position: absolute; left: -9999px; } 

这是jquery代码。

  function setupLabel() { if ($('.label_check input').length) { $('.label_check').each(function(){ $(this).removeClass('c_on'); }); $('.label_check input:checked').each(function(){ $(this).parent('label').addClass('c_on'); }); }; if ($('.label_radio input').length) { $('.label_radio').each(function(){ $(this).removeClass('r_on'); }); $('.label_radio input:checked').each(function(){ $(this).parent('label').addClass('r_on'); }); }; }; $(document).ready(function(){ $('.label_check, .label_radio').click(function(){ setupLabel(); }); setupLabel(); }); 

请告诉我这段代码有什么问题,非常感谢帮助。

谢谢。

我真的建议使用jquery.uniform (因为你已经在使用jQuery,或任何相同的替代方案)来设置复选框,广播和选择等表单元素。 除了将图像放在“真正的”表单元素上方之外,还有更多内容,例如处理标签上的点击等。

如果您使用的是jquery.uniform,则可以动态选择单选按钮。

设置表单元素的值后,例如

 $("[name='buttongroup']['value='male']").attr("checked", true); 

…只需执行以下操作即可更新单选按钮选择的显示:

 $.uniform.update(); 

您的问题是,您为所有输入使用相同的ID,ID是唯一的。 这就是为什么它改变了id =“q_0”的第一个实例