谁能帮我解决这个问题?
$(document).ready(function() {
if(
$("input.check").is(":checked")){
$(this).parent().addClass("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>