每当检查Checkbox sec_dept_time_leave并将段落标签选中时,我想在标签中添加一个名为“必需”的类。
未选中时,删除标签和段落标签的“必需”类。
这是我的jQuery:
$('#sec_dept_time_leave').on("click", function() {
if ($(this).is(":checked")) {
$('label#hr_dept_time_orgn1').addClass("required");
}
else {
$('label#hr_dept_time_orgn1').removeClass("required");
}
});
HTML:
<div class="row singlecheck">
<label for="sec_dept_time_leave">Department Time/Leave Entry</label>
<input value="Department time" type="checkbox"
name="sec_dept_time_leave" id="sec_dept_time_leave" />
</div>
<div class="row">
<label for="hr_dept_time_orgn1">ORGN 1</label>
<input type="text" name="hr_dept_time_orgn1" id="hr_dept_time_orgn1" />
</div>
<p> This is required text </p>
<ul>
<li><label for="hr_dept_time">Time</label><input value="Dept Time" type="radio" name="hr_dept_time_leave" id="hr_dept_time" /></li>
<li><label for="hr_dept_leave">Leave</label><input value="Dept Leave" type="radio" name="hr_dept_time_leave" id="hr_dept_leave" /></li>
<li><label for="hr_dept_both">Both</label><input value="Both Dept Time and Leave" type="radio" name="hr_dept_time_leave" id="hr_dept_both" /></li>
</ul>
我希望这是检查时的最终结果
<div class="row required">
<label for="hr_dept_time_orgn1">ORGN 1</label>
<input type="text" name="hr_dept_time_orgn1" id="hr_dept_time_orgn1" />
</div>
<p> This is required text<span class="required">*</span></p>
当它被选中然后取消选中时,我希望它作为最终结果
<div class="row">
<label for="hr_dept_time_orgn1">ORGN 1</label>
<input type="text" name="hr_dept_time_orgn1" id="hr_dept_time_orgn1" />
</div>
<p> This is required text</p>
<ul>
<li><label for="hr_dept_time">Time</label><input value="Dept Time" type="radio" name="hr_dept_time_leave" id="hr_dept_time" /></li>
<li><label for="hr_dept_leave">Leave</label><input value="Dept Leave" type="radio" name="hr_dept_time_leave" id="hr_dept_leave" /></li>
<li><label for="hr_dept_both">Both</label><input value="Both Dept Time and Leave" type="radio" name="hr_dept_time_leave" id="hr_dept_both" /></li>
</ul>
非常感谢