2017-01-04 13 views
0

このグループ内の入力に注目すると、フォームグループの親を取得できないのはなぜですか?入力フォーカスで、私はコンソールで返されたFirefoxでオブジェクトを取得しています:Object {length:1、prevObject:Object、context:、1 more ...}しかし、これは私が興味を持っているオブジェクトではありません。 )は同じHTMLノードを返しています。私はconsole.log($(this).parent());コードをinputFocusメソッドの内部で使用しています。JQueryで入力フォーカスの親を取得できませんか?

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(); 
+2

は、あなたが何をしようとしているのdiv親ノードを取得するために$(this).parent()[0];していますか? –

+1

'$(document).ready()'は、ページがロードされたときに一度だけ起動します。グローバルドキュメントのスコープ内に存在しなければなりません機能 – mike510a

+0

@PraveenKumar - バニラJSとjQueryを混在させても何も間違っていない...。) – talemyn

答えて

0

それはあなたが取得しようとしているノードのオブジェクトを取得しています。

次持つ二つのオプションの1:

  1. バニラJSとjQueryを混合
  2. またはthis.parentNode;
+0

ITYM 'this.parentNode()'はDOMメソッドなので、 – Barmar

+0

編集のヒントありがとうございます。 – akiespenc

+0

@akiespenc分かりません: '$(this).parentNode();'が動作するかもしれません。あなたは 'thisparentNode;を実行する必要があります。 –

関連する問題