我的页面上有三个单选按钮和一个文本输入:
<div class="row-fluid" style="padding-bottom: 5px;">
<label class="radio inline"><input type="radio" name="optionsRadios" id="optionsRadios1" value="Name" checked>Search By Name </label>
<label class="radio inline"><input type="radio" name="optionsRadios" id="optionsRadios2" value="City">Search By City </label>
<label class="radio inline"><input type="radio" name="optionsRadios" id="optionsRadios3" value="Code">Search By code </label>
</div>
<div class="row-fluid">
<span class="row-fluid">
<input type="text" id="txtSearch" name="txtSearch" />
<input type="button" id="btnSubmit" value="Search" class="btn btn-primary" />
</span>
</div>
我正在尝试根据所选的单选按钮验证txtSearch上输入的长度,因为这些列的数据库字段长度不同:
已更新:
$("#frmSearch").validate({
debug: false,
rules: {
'txtSearch': {
required: true,
maxlength: function () {
var sel = $('input[name=optionsRadios]:checked', '#frmSearch').val();
if (sel == 'City') {
return 25;
}
else {
return 50;
}
},
minlength: 2
}
}
});
我将警报放入告诉我值的长度,所有警报都返回我的预期,但每个条目上显示的验证消息,无论长度如何,都是“请输入不超过1个字符。 “
答案 0 :(得分:1)
删除depends
并替换为:
maxlength: (function(){
var sel = $('input[name=optionsRadios]:checked', '#frmOfficeSearch').val();
if (sel == 'Name') {
return 50;
}
if (sel == 'City') {
return 25;
}
if (sel == 'Code') {
return 50;
}
})()
这将创建一个自执行函数,该函数根据所选的无线电返回正确的整数。