【发布时间】:2018-07-04 13:47:00
【问题描述】:
我有一个复选框,选中时会显示一个隐藏的组合框。当我提交表单并且未按下复选框(组合框仍然隐藏)时,它仍然将隐藏的组合框作为“必填”字段。这是我的代码:
$(function() {
var checkbox = $("#hascustpo");
var hidden = $("#custpo");
hidden.hide();
checkbox.change(function() {
if (checkbox.is(':checked')) {
checkbox.attr("required", true);
hidden.show();
} else {
hidden.hide();
checkbox.attr("required", false);
}
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<label>
<input type="checkbox" name="hascustpo" value="True" id="hascustpo"/>
Has Customer PO
</label><br>
<div id="custpo">
<label>Customer PO Number <b style="color:red;">*</b></label>
<select id="txtfield" style="width: 200px;" name="custpo" class="inputvalues" required>
<option disabled selected>-- Customer PO Number</option>
<option>AUTO PO NUM | DESCRIPTION</option>
</select>
<br>
</div>
【问题讨论】:
-
尝试使用 checkbox.removeAttr("required");而是 checkbox.attr("required",false);
标签: javascript jquery html checkbox