我有一个 HTML 表单,并且正在使用 JQuery Validate 插件来要求某些表单字段为必填字段。我有一个带有 3 个选项的单选按钮字段:
小时
天
不确定
另一个字段用于输入与小时或天选择相对应的数字。但是,如果用户选择“不确定”,则不需要输入数字。如果用户不选择“不确定”(即当他们选择“小时”或“天”时),我无法弄清楚如何仅使数字字段成为必填字段。这是我现在的 HTML:
<form role="form" action="continue.php" method="post" id="durationForm">
<tr>
<td colspan = "3">Please enter the duration?</td>
</tr>
<tr>
<td width="10%"><input type="number" class="form-control" id="duration" name="durationNumber" required></td>
<label for="duration" class="validateError"></label>
<td width="50%">
<div class="controls">
<label class="radio">
<input type="radio" name="duration" id="duration" value="Hours" required> Hours</label>
<label class="radio">
<input type="radio" name="duration" id="duration" value="Days" required> Days</label>
<label class="radio">
<input type="radio" name="duration" id="duration" value="Unsure" required> Unsure</label>
<label for="duration" class="validateError"></label>
</div>
</td>
</tr>
<button type="submit" class="btn btn-primary">Next</button>
</form>
这是脚本:
$(document).ready(function () {
$("#durationForm").validate({
errorClass: "validateError"
});
});
我已经设置了一个jsfiddle http://jsfiddle.net/xJBX9/这也表明了这一点。
From: https://stackoverflow.com/a/19546185/1477051 https://stackoverflow.com/a/19546185/1477051
jQuery Validate 实际上直接支持这一点,使用依赖性
表达式 http://jqueryvalidation.org/required-method.
您需要做的就是更改验证选项,如下所示:
$('#myform').validate({
rules: {
fieldA: {
required:'#checkA:checked'
}
}
});
就是这样!
我在你的代码中使用了它:http://jsfiddle.net/xJBX9/1/ http://jsfiddle.net/xJBX9/1/
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)