利用JS验证查询参数-选择月份后必须选择年份

时间:2022-12-31 02:22:58

js代码:

function queryAgentInfo(){
// 标记
var flag=false;
//遍历所有月份
$(".month").each(function(){
if($(this).hasClass("LabelSelected")){
flag = true;
}
});
//获取选中的年份
if(flag){
var yearList= $('input:radio[name="createTimeYear"]:checked').val();
if(yearList==null || yearList == ""){
alert("请选择一个年份!");
return false;
}
} $("#result_list").empty();
$("#result_list").load("${ctx}/agent/queryAgentInfo.do",$("#agent_seach_form").serializeJson());
}

HTML代码:

 <dl class="clearfix">
<dt>年份:</dt>
<dd>
<span><input name="createTimeYear" type="radio" value="" checked="checked">不限</span>
<span><input id="1"name="createTimeYear" type="radio" value="2015">2015年</span>
<span><input id="2" name="createTimeYear" type="radio" value="2016">2016年</span>
</dd>
</dl>
<dl class="clearfix">
<dt>月份:</dt>
<dd>
<span>
<input id="a_0" name="createTimeMonths" type="checkbox" value="1,2,3" class="CheckBoxClass">
<label for="a_0"class="CheckBoxLabelClass">不限</label>
</span>
<span>
<input id="a_1" name="createTimeMonths"type="checkbox" value="1" class="CheckBoxClass">
<label class="CheckBoxLabelClass month" for="a_1">1月</label>
</span>
<span>
<input id="a_2" name="createTimeMonths" type="checkbox" value="2" class="CheckBoxClass">
<label class="CheckBoxLabelClass month" for="a_2">2月</label>
</span>
<span>
<input id="a_3" name="createTimeMonths" type="checkbox" value="3" class="CheckBoxClass">
<label class="CheckBoxLabelClass month" for="a_3">3月</label>
</span>
</dd>
</dl>