如果单选按钮被选中,则 jQuery addClass

发布于 2024-12-13 02:44:29 字数 1264 浏览 5 评论 0原文

谁能帮我解决这个问题吗?

$(document).ready(function() {
    if(
        $("input.check").is(":checked")){
            $(this).parent().addClass("question-box-active");
    }
});

我试图将一个类(question-box-active)添加到 .check (单选按钮)的父级(仅当它被选中时)。如果我用警报进行测试,效果很好,但我无法添加该类。

感谢大家的帮助。

更新:这是表单的 HTML。我只复制了 48 个 div 中的 3 个。正如您可能猜测的那样,如果单选按钮已在页面加载时选中,我会尝试将一个类添加到问题框中。到目前为止还没有运气。

<div class="question-box">
    <input class="check" type="checkbox" name="question-1" value="1" <?php if(isset($_POST['question-1'])) echo "checked"; ?> />
    <span class="question">1. Save a rainforest or grow organic vegetables</span>
</div>

<div class="question-box alt">
    <input class="check" type="checkbox" name="question-2" value="1" <?php if(isset($_POST['question-2'])) echo "checked"; ?> />
    <span class="question">2. Solve complicated math problems</span>
</div>

<div class="question-box">
    <input class="check" type="checkbox" name="question-3" value="1" <?php if(isset($_POST['question-3'])) echo "checked"; ?> />
    <span class="question">3. Act in a movie, play, or television show</span>
</div>

Can anyone help me with this problem?

$(document).ready(function() {
    if(
        $("input.check").is(":checked")){
            $(this).parent().addClass("question-box-active");
    }
});

I'm trying to add a class (question-box-active) to the parent of .check (a radio button) only if it is checked. It works fine if I test with an alert, but I can't manage to add that class.

Thanks all for the help.

Update: This is the HTML for the form. I copied only 3 divs out of the 48. As you may have guesses I'm trying to add a class to question-box if the radio button is already checked on page load. So far no luck.

<div class="question-box">
    <input class="check" type="checkbox" name="question-1" value="1" <?php if(isset($_POST['question-1'])) echo "checked"; ?> />
    <span class="question">1. Save a rainforest or grow organic vegetables</span>
</div>

<div class="question-box alt">
    <input class="check" type="checkbox" name="question-2" value="1" <?php if(isset($_POST['question-2'])) echo "checked"; ?> />
    <span class="question">2. Solve complicated math problems</span>
</div>

<div class="question-box">
    <input class="check" type="checkbox" name="question-3" value="1" <?php if(isset($_POST['question-3'])) echo "checked"; ?> />
    <span class="question">3. Act in a movie, play, or television show</span>
</div>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

笑叹一世浮沉 2024-12-20 02:44:29

尝试类似的东西

$('input.check').is(':checked').each(function() { 
    $(this).parent().addClass('question-box-active');
});

Try something like

$('input.check').is(':checked').each(function() { 
    $(this).parent().addClass('question-box-active');
});
不知所踪 2024-12-20 02:44:29

我认为 $(this) 在您的情况下并不是指单选按钮

您可以绑定单选按钮的单击事件并决定从那里做什么...

$(document).ready(function(){
    $("input.check").click(function(){
        if($(this).is(":checked")){
            $(this).parent().addClass("question-box-active");
        }
    });
});

I think $(this) is not referring to the radiobutton in your case

You could bind the click event of the radio button and decide what to do from there...

$(document).ready(function(){
    $("input.check").click(function(){
        if($(this).is(":checked")){
            $(this).parent().addClass("question-box-active");
        }
    });
});
各自安好 2024-12-20 02:44:29

您使用的 this 位于 ready 处理程序回调的函数上下文中,因此它并不像您预期​​的那样指向 input.check

尝试这样的事情:

$(document).ready(function() {

    var foo = $('input.check');

    if ( foo.is(':checked') ) {
        foo.parent().addClass('question-box-active');
    }

});

这是假设你的代码逻辑是正确的。然而,我认为您正在尝试做与道格在另一个答案中提供的类似的事情。发布一些 HTML 可能有助于澄清。

The this you're using is within the function context of the ready handler callback, so it's not pointing to input.check as you expected.

Try something like this instead:

$(document).ready(function() {

    var foo = $('input.check');

    if ( foo.is(':checked') ) {
        foo.parent().addClass('question-box-active');
    }

});

This is assuming your code logic is correct. I assume, however, that you're trying to do something similar to what Doug provided in another answer. Posting some HTML might help clarify.

似最初 2024-12-20 02:44:29

这会起作用。

$(document).ready(function() {

    var foo = $('input.check:checked');

    if ( foo.is(':checked') ) {
        foo.parent().addClass('question-box-active');
    }
});

This will work.

$(document).ready(function() {

    var foo = $('input.check:checked');

    if ( foo.is(':checked') ) {
        foo.parent().addClass('question-box-active');
    }
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文