JavaScript-input标签添加必填校验
源
<div class="form-group has-error has-feedback"> <label class="control-label" for="inputError2">Input with error</label> <input type="text" class="form-control" id="inputError2" aria-describedby="inputError2Status"> <span class="glyphicon glyphicon-remove form-control-feedback" aria-hidden="true"></span> <span id="inputError2Status" class="sr-only">(error)</span> </div>
例:
<div class="form-group"> <label class="control-label" for="taskName" style="display: none">不能为空!</label> <input type="text" class="form-control" name="taskName" id="taskName" aria-describedby="inputError2Status"/> <%--<input type="text" class="form-control" id="inputError2" aria-describedby="inputError2Status">--%> <span class="glyphicon glyphicon-remove form-control-feedback" aria-hidden="true"></span> <span id="inputError2Status" class="sr-only">(error)</span> </div>
,,,
function isRequired(){ var flag = false; $(".control-label").each(function(){ var inputval = $("#"+$(this).attr("for")+"").val(); if(inputval==null||inputval==""||inputval==undefined){ $(this).attr("style","display:show"); $("#"+$(this).attr("for")+"").parent().attr("class","form-group has-error has-feedback"); //alert(false); flag = false; }else{ $(this).attr("style","display:none"); $("#"+$(this).attr("for")+"").parent().attr("class","form-group "); //alert(true); flag = true; } }); return flag; }