gpt4 book ai didi

javascript - 标记复选框后的必填字段

转载 作者:行者123 更新时间:2023-11-28 09:15:35 25 4
gpt4 key购买 nike

我对 JavScript 非常陌生,我正在尝试创建一个表单,要求在标记复选框后通过切换所需的类将信息输入到字段中。我已经尝试了两天,但如果选中“体育馆”框,则无法将“开始日期”设为必填字段。任何帮助都会很棒。

这是我到目前为止所拥有的:

    <!DOCTYPE html>
<html>
<head>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.js"></script>
<script type="text/javascript">

$(document).ready(function(){
$("#gym").change(function(){
$("#togg1").toggle(this.checked);
$("#startDate").toggleClass("required");
$("#endDate").toggleClass("required");
$("#startTime").toggleClass("required");
$("#endTime").toggleClass("required");

if(this.checked){
$("#startDate").attr('required','required');
$("#endDate").attr('required','required');
$("#startTime").attr('required','required');
$("#endTime").attr('required','required');
}
else{
$("#startDate").removeAttr('required')
$("#endDate").removeAttr('required')
$("#startTime").removeAttr('required')
$("#endTime").removeAttr('required')
}

});
});

</script>


</head>
<body>


<div id="courseForm" style="width: 500px; border: 1px solid #000; padding: 50px 0 50px 50px;margin: 0 auto;">
<form id="serviceForm" action="#" onSubmit="required(this);" method="POST" enctype="text/plain">
<input name="orgid" type="hidden" value="000001" />
<input name="retURL" type="hidden" value="#" />
<label for="company">* Agency Requesting Facility:</label>
<br />
<input id="company" required="required" maxlength="50" name="company" size="50" type="text" />
<br />
<br />
<label for="name">* Agency Contact:</label>
<br />
<input id="name" required="required" maxlength="50" name="name" size="50" type="text" />
<br />
<br />
<label for="phone">* Contact Phone Number:</label>
<br />
<input id="phone" required="required" maxlength="14" name="phone" size="14" type="text" />
<br />
<br />
<br />Courses offered
<br />

<input id="gym" name="gym" type="checkbox" value="1" />Gymnasium
<br />
<span id="togg1" style="display:none">
<span id="togg2" style="display:inline">
<lable for="startDate">* Start Date:</lable>
<input id="startDate" maxlength="10" name="startDate" size="10" />
</span>
<span id="togg2" style="display:inline">
<lable for="endDate">* End Date:</lable>
<input id="endDate" maxlength="10" name="endDate" size="10" /><br /><br />
</span>
<span id="togg2" style="display:inline">
<lable for="startTime">* Start Time:</lable>
<input for="startTime" maxlength="7" name="startTime" size="7" />
</span>
<span id="togg2" style="display:inline">
<lable for="endTime">* End Time:</lable>
<input for="endTime" maxlength="7" mane="endTime" size="7" /><br />
</span>
</span>

<br />
<br />
<label for="description">Comments:</label>
<br />
<textarea name="description" cols="40" rows="5"></textarea>
<br />
<br />* denotes required fields
<br />
<input type="submit" name="submit" id="submit" value="SUBMIT" />
</form>
</div>
</body>
</html>

最佳答案

您忘记将“必需”属性添加到“开始日期”

$(document).ready(function(){
$("#gym").change(function(){
$("#togg1").toggle(this.checked);
$("#startDate").toggleClass("required");

if(this.checked){
$("#startDate").attr('required','required');
}else{
$("#startDate").removeAttr('required')
}

});
});

关于javascript - 标记复选框后的必填字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15709921/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com