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;
}

  

posted @ 2022-03-23 16:10  甜8筒  阅读(1503)  评论(0编辑  收藏  举报