jQuery最近的问题()和addClass()

时间:2018-11-15 04:54:39

标签: jquery

我现在陷入困境,我们将不胜感激。我在Bootstrap模式中有此表单:

<form action="" method="post" id="UserForm" class="form-horizontal">
<div class="modal-body">
    <div class="form-group">
        <label for="iUserName" class="col-sm-3 control-label">User Name</label>
        <div class="col-sm-9">
            <input type="text" class="form-control" name="iUserName" id="iUserName" placeholder="User Name" value="" />
        </div>
    </div>

    <div class="form-group">
        <label for="iPassword" class="col-sm-3 control-label">Password</label>
        <div class="col-sm-9">
            <input type="password" class="form-control" name="iPassword" id="iPassword" placeholder="iPassword" value="" />
        </div>
    </div>

    <div class="form-group">
        <label for="iConfPassword" class="col-sm-3 control-label">Confirm Password</label>
        <div class="col-sm-9">
            <input type="password" class="form-control" name="iConfPassword" id="iConfPassword" placeholder="Confirm Password" value="" />
        </div>
    </div>

    <div class="form-group">
        <label for="iRoleSelect" class="col-sm-3 control-label">Role</label>
        <div class="col-sm-9">
            <select class="form-control select2" id="iRoleSelect" name="iRoleSelect" style="width: 100%;"></select>
        </div>
    </div>
</div>
<div class="modal-footer">
    <button type="button" class="btn btn-info pull-left" data-dismiss="modal">Cancel</button>
    <button type="submit" class="btn btn-info pull-right">Save</button>
</div>
</form>

这是我的JS:

$('#UserForm').on('submit', function () {
        var flag_UserName, flag_Password, flag_Role
        var emptyInput = $('#UserForm').find('input[type=text], input[type=password], select').val('')
        for (var i = 0; i < emptyInput.length; i++) {
            var div = emptyInput[i].closest('.form-group').addClass('has-warning')
            console.log(div)
            return false;
        }
    })

我想做的是找到表单内的每个空输入,然后向该输入的最接近的div添加一个新的“具有警告”类。我当前的代码给我这个错误:

  

TypeError: emptyInput[i].closest(...).addClass is not a function

2 个答案:

答案 0 :(得分:2)

我希望你尝试这样做

z = np.zeros(xx.shape)
np.sqrt(r**2 - xx**2 - yy**2, where=(r**2 - xx**2 - yy**2)>=0, out=z)
$('#UserForm').on('submit', function () {      
      var flag_UserName, flag_Password, flag_Role   
      var emptyInput = $('#UserForm').find('input[type=text], input[type=password], select');
 
      for (var i = 0; i < emptyInput.length; i++) { 
           $(emptyInput[i]).closest('.form-group').removeClass('has-warning')
           if($(emptyInput[i]).val()=='' || $(emptyInput[i]).val()==null){
              var div = $(emptyInput[i]).closest('.form-group').addClass('has-warning')
              return false;
              break;
          }            
      }     
});

答案 1 :(得分:1)

尝试使用$符号。 工作示例:

$('#UserForm').on('submit', function () {
        var flag_UserName, flag_Password, flag_Role
        var emptyInput = $('#UserForm').find('input[type=text], input[type=password], select').val('');
        for (var i = 0; i < emptyInput.length; i++) {
            var div = $(emptyInput[i]).closest('.form-group').addClass('has-warning')
            console.log(div)
        }
        return false;
    })
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form action="" method="post" id="UserForm" class="form-horizontal">
<div class="modal-body">
    <div class="form-group">
        <label for="iUserName" class="col-sm-3 control-label">User Name</label>
        <div class="col-sm-9">
            <input type="text" class="form-control" name="iUserName" id="iUserName" placeholder="User Name" value="" />
        </div>
    </div>

    <div class="form-group">
        <label for="iPassword" class="col-sm-3 control-label">Password</label>
        <div class="col-sm-9">
            <input type="password" class="form-control" name="iPassword" id="iPassword" placeholder="iPassword" value="" />
        </div>
    </div>

    <div class="form-group">
        <label for="iConfPassword" class="col-sm-3 control-label">Confirm Password</label>
        <div class="col-sm-9">
            <input type="password" class="form-control" name="iConfPassword" id="iConfPassword" placeholder="Confirm Password" value="" />
        </div>
    </div>

    <div class="form-group">
        <label for="iRoleSelect" class="col-sm-3 control-label">Role</label>
        <div class="col-sm-9">
            <select class="form-control select2" id="iRoleSelect" name="iRoleSelect" style="width: 100%;"></select>
        </div>
    </div>
</div>
<div class="modal-footer">
    <button type="button" class="btn btn-info pull-left" data-dismiss="modal">Cancel</button>
    <button type="submit" class="btn btn-info pull-right">Save</button>
</div>
</form>