如果单选按钮被选中,则 jQuery addClass
谁能帮我解决这个问题吗?
$(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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
尝试类似的东西
Try something like
我认为
$(this)
在您的情况下并不是指单选按钮您可以绑定单选按钮的单击事件并决定从那里做什么...
I think
$(this)
is not referring to the radiobutton in your caseYou could bind the click event of the radio button and decide what to do from there...
您使用的
this
位于ready
处理程序回调的函数上下文中,因此它并不像您预期的那样指向input.check
。尝试这样的事情:
这是假设你的代码逻辑是正确的。然而,我认为您正在尝试做与道格在另一个答案中提供的类似的事情。发布一些 HTML 可能有助于澄清。
The
this
you're using is within the function context of theready
handler callback, so it's not pointing toinput.check
as you expected.Try something like this instead:
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.
这会起作用。
This will work.