我对 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>