无法在JQuery中获得输入焦点父级?

时间:2017-01-04 21:04:54

标签: javascript jquery

为什么当我专注于此组内的输入时,我无法获取表单组的父级?在输入焦点上,我在控制台中返回Firefox中的对象:对象{length:1,prevObject:Object,context:,1 more ...}但是,这不是我感兴趣的对象。这个和.parent( )返回相同的HTML节点。我正在使用inputFocus方法中的console.log($(this).parent());代码。

HTML:

<div class="form-group repeat-group" id="something" data-num="1">
  <div class="col-md-5 col-sm-5 col-xs-12 ">
  <input type="text" id="description" name="desc" required="required" class="form-control col-md-7 col-xs-12">
  </div>
</div>

使用Javascript:

var formAutoGenerator = function() {
    var self = this; //$ has it's own 'this'
    this.repeatGroup = $('.repeat-group');
    this.formGroupLength = this.repeatGroup.length;
    this.init = function(){
        this.inputFocus(); 
        this.correctClassAdded();
    };
    this.classTag = 'repeat-group'; 
    this.correctClassAdded = function(formGroup){
       // console.log( $(formGroup).offsetParent() );
        return formGroup; 
    };
    this.inputFocus = function() {
        $(document).ready(function(){

        $('.repeat-group input').on('focus', function(e) {
                if ( self.correctClassAdded( $(this) ) ) {
                    console.log('working');

                } 
                console.log($(this).parent());
            var group = $(this).parent().siblings().children();
            var groupInputValArray = []; 
            var emptyCounter = 0; 
            for ( a = 0; a <= group.length -1; a++){
                //if the input does not have a value add null
                if ( group[a].value === ""){ emptyCounter++; }
                groupInputValArray.push(group[a].value);
            }
            //if the array contains only one null then add another form group
            if ( emptyCounter == 1){
                //then we will go ahead and add our new group
            }
               }); 
        });
    }
    this.appendGroup = function(){

    }; 
    this.init(); 
}

var newfields = new formAutoGenerator();

1 个答案:

答案 0 :(得分:0)

它正在检索您想要获取的节点的对象。

您有两种选择之一:

  1. 执行$(this).parent()[0];以获取div父节点
  2. this.parentNode;