我正在使用JQuery Validation插件(http://jqueryvalidation.org)来验证html表单。我想只有当下拉菜单选择值为0时才需要textarea字段。任何帮助都会很棒。提前谢谢!
以下是表单的内容:
<form name="reviewProduct" id="reviewProduct" method="POST">
<select id="reaction" name="reaction">
<option value="1">Yes. I like it.</option>
<option value="0">No. I don't like it.</option>
</select>
<textarea id="explainwhy" name="explainwhy" rows="4"></textarea>
</form>
<script>
$(document).ready(function () {
$('#reviewProduct').validate({
rules: {
explainwhy: {
required: {
depends: function () {
return $('#reaction').val() == "0";
}
}
}
}
});
});
</script>
答案 0 :(得分:1)
使用depends
方法使required
规则“依赖于”一系列条件。
$(document).ready(function () {
$('#reviewProduct').validate({
rules: {
explainwhy: {
required: {
depends: function () {
return $('#reaction').val() == "0";
}
}
},
reaction: {
required: true
}
}
});
});