大家好。我不是一个很好的程序员,但仍然需要做一些编码。
现在我在使用 jQuery 更改单选按钮图像时遇到问题。
我已经在网上找到了这段代码,但它仍然不起作用。
它更改了单选按钮图像,但是当我尝试选择时,它仅更改第一个单选框的图片。(每次)
这是我的 HTML 代码:
<div id="testcontent">
<div class="questions">
<div id="question0" class="question">
<span class="qutitle">asdg asdg</span>
<FIELDSET class="radios">
<label for="q_0" class="label_radio">
<input id="q_0" type="radio" name="q_0" value="2">dgasdg
</label>
<label for="q_0" class="label_radio">
<input id="q_0" type="radio" name="q_0" value="0"> sdgas
</label>
<label for="q_0" class="label_radio">
<input id="q_0" type="radio" name="q_0" value="1">dgas
</label>
</FIELDSET>
</div>
这是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 http://pixelmatrixdesign.com/uniform/(因为您已经在使用 jQuery 或任何同等的替代方案)来设置表单元素的样式,例如复选框、单选和选择。
它不仅仅是将图像放置在“真实”表单元素上方,例如处理标签上的点击等。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)