选中单选框上的 Jquery addClass
我检查了所有主题,但我根本不知道为什么我的脚本不起作用:(
<script type="text/javascript">
$('input[name=pic]').click(function() {
$(this).closest('ul').find(".green").removeClass("green");
if($(this).is(':checked')) {
$(this).closest('ul').find("li").addClass('green');
}
});
</script>
another script what is making the li tags
var link = document.createElement('li');
var parts = result.tbUrl.split('::');
link.innerHTML = "<label for='asd"+i+"'><img src='"+result.tbUrl+"' /></label><br /><input id='asd"+i+"' type='radio' name='pic' value='http://"+parts[1]+"' />";
contentDiv.appendChild(link);
etc...
<ul>
<li><input type="radio" name="pic" value="asd"/>asd</li>
<li><input type="radio" name="pic" value="b"/>asd</li>
<li><input type="radio" name="pic" value="ba"/>asd</li>
<li><input type="radio" name="pic" value="bs"/>asd</li>
<li><input type="radio" name="pic" value="bc"/>asd</li>
</ul>
请帮助我!
I checked all the topics, but i simply don't know why my script does not work :(
<script type="text/javascript">
$('input[name=pic]').click(function() {
$(this).closest('ul').find(".green").removeClass("green");
if($(this).is(':checked')) {
$(this).closest('ul').find("li").addClass('green');
}
});
</script>
another script what is making the li tags
var link = document.createElement('li');
var parts = result.tbUrl.split('::');
link.innerHTML = "<label for='asd"+i+"'><img src='"+result.tbUrl+"' /></label><br /><input id='asd"+i+"' type='radio' name='pic' value='http://"+parts[1]+"' />";
contentDiv.appendChild(link);
etc...
<ul>
<li><input type="radio" name="pic" value="asd"/>asd</li>
<li><input type="radio" name="pic" value="b"/>asd</li>
<li><input type="radio" name="pic" value="ba"/>asd</li>
<li><input type="radio" name="pic" value="bs"/>asd</li>
<li><input type="radio" name="pic" value="bc"/>asd</li>
</ul>
Please help me!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
$('#pic')
是一个 ID 选择器,您的输入没有 ID。您可能指的是'$('input[name=pic]')
。此外,您将
green
类应用于,但随后尝试在内部查找
.green
元素/em>.parents('li')
。也许您想要$(this).closest('ul')
代替?$('#pic')
is an ID selector, and your inputs don't have IDs. You probably meant'$('input[name=pic]')
.Also, you're applying the
green
class to the<li>
, but then trying to find.green
elements inside the.parents('li')
. Maybe you want$(this).closest('ul')
instead?博宾斯是正确的。您需要绑定 $(document).ready
我也使脚本更加高效。
bobince is correct. You need to bind in a $(document).ready
I have made the script more efficient too.