我有以下的jQuery代码:
<script> function hideMenteeQuestions() {
$("#menteeapp").hide();
$("textarea[name='short_term_goals']").rules("remove", "required");
$("textarea[name='long_term_goals']").rules("remove", "required");
}
function showMenteeQuestions() {
$("#menteeapp").show();
$("textarea[name='short_term_goals']").rules("add", {
required: true
});
$("textarea[name='long_term_goals']").rules("add", {
required: true
});
}
function hideMentorQuestions() {
$("#mentorapp").hide();
$("input[name='mentees']").rules("remove", "required");
}
function showMentorQuestions() {
$("#mentorapp").show();
$("input[name='mentees']").rules("add", {
required: true
});
}
</script>
<script>
$(document).ready(function(){
$("#mentee").change(function(){
if($(this).is(':checked')){
showMenteeQuestions();
}else{
hideMenteeQuestions();
}
});
$("#mentor").change(function(){
if($(this).is(':checked')){
showMentorQuestions();
}else{
hideMentorQuestions();
}
});
$('#mentee').change();
$('#mentor').change();
});
</script >
此外,这是我的复选框的HTML代码:
<input type="checkbox" name="type[]" id="mentor" value="mentor"><span class="checkbox">Mentor</span>
<input type="checkbox" name="type[]" id="mentee" value="mentee"><span class="checkbox">Mentee</span>
这段代码的作用是根据所选复选框来隐藏特定的
元素。当您单击复选框时,它可以正常工作。但是,我还想在页面加载时触发更改函数。出于某种原因,它只调用第一个更改函数,即
#mentee
的情况。如果我更改顺序,则另一个函数起作用。它从未进入第二个change()
调用。
有什么想法吗?
$("#mentee")
里的if语句应该被注释掉吗? - JesseBueskingshowMenteeQuestions
,hideMenteeQuestions
(因为脚本在第一个alert
之后停止工作,所以我会猜测是其中一个)。 - Jasper