选中单选框上的 Jquery addClass

发布于 2024-08-29 07:13:28 字数 1120 浏览 2 评论 0原文

我检查了所有主题,但我根本不知道为什么我的脚本不起作用:(

<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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

屋檐 2024-09-05 07:13:28

$('#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?

    囍笑 2024-09-05 07:13:28

    博宾斯是正确的。您需要绑定 $(document).ready

    我也使脚本更加高效。

    <script type="text/javascript">
        $(document).ready(function() {
            $(':radio[name=pic]').click(function() {           
                $(this).siblings().removeClass("green").end()
               .addClass("green"); //a click always makes a radio button true, so no need to check
            });
        });
    </script>
    

    bobince is correct. You need to bind in a $(document).ready

    I have made the script more efficient too.

    <script type="text/javascript">
        $(document).ready(function() {
            $(':radio[name=pic]').click(function() {           
                $(this).siblings().removeClass("green").end()
               .addClass("green"); //a click always makes a radio button true, so no need to check
            });
        });
    </script>
    
    ~没有更多了~
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文