2016-09-29 7 views
3

これを入れる方法はわかりません。これはちょっと乱雑です。私は質問をするために書く言葉を使うことができませんでした。ここに私のコードは次のとおりです。jqueryを使用してネストされたdivからIDを取得

<div id="addfieldcontainerdropdown"> 
 
    <div id="addDropdownfield"> 
 
     <div id="div0"> 
 
      <select name="0" style="margin: 20px" id="0"> 
 
       <option value="1">1</option> 
 
       <option value="2">2</option> 
 
      </select> 
 
      <input style="margin: 7px" id="1" placeholder="Value" onkeyup="check_if_value_set(this); return false;" 
 
        type="text"> 
 

 
      <span style="margin: 7px;cursor: pointer;" onclick="remove_fields(this);return false;"><iclass="fa fa-times"></i></span> 
 
     </div> 
 

 
     <div id="div2"> 
 
      <select name="2" style="margin: 20px" id="2"> 
 
       <option value="1">1</option> 
 
       <option value="2">2</option> 
 
      </select> 
 
      <input style="margin: 7px" id="3" placeholder="Value" onkeyup="check_if_value_set(this); return false;" 
 
        type="text"> 
 

 
      <span style="margin: 7px;cursor: pointer;" onclick="remove_fields(this);return false;"><i class="fa fa-times"></i></span> 
 
     </div> 
 

 
     <div id="div4"> 
 
      <select name="4" style="margin: 20px" id="4"> 
 
       <option value="1">1</option> 
 
       <option value="2">2</option> 
 
      </select> 
 
      <input style="margin: 7px" id="5" placeholder="Value" onkeyup="check_if_value_set(this); return false;" 
 
        type="text"> 
 
      <span style="margin: 7px;cursor: pointer;" onclick="remove_fields(this);return false;"><i class="fa fa-times"></i></span> 
 
     </div> 
 

 
    </div> 
 

 
    <a href="javascript:void(0)" onclick="show_new_text_field(this);event.preventDefault();return false;" style="cursor:pointer"> 
 

 
    <span id="add_new_span" class="addnew" style="float: none;margin-left: 1%;"><i class="fa fa-plus fa-fw"></i>Add New Field</span> 
 

 
    </a> 
 
</div>

あなたは、コードの下部にある「show_new_text_field(この)」という名前の関数があると見ることができます。この関数を使うと、div4というIDのIDを取得する必要があります。これはどのようにID4のボックスを選択するのですか?私はprent()とclosest()を使って試しました。何も働かなかった。任意のタイプの提案が評価されます。 ありがとうございます!

+0

var lastDiv = $("[id^=div]").last(); //Get Last div whose ID begins with "div" var lastDivId = lastDiv.attr('id'); //Get ID of that div var firstSelect = lastDiv.find('select').first(); //Get the first 'select' of that div var firstSelectId = firstSelect.attr('id'); //Get that select's ID console.log("Last Div: " + lastDivId); // "Last Div: div4" console.log("First Select in " + lastDivId + ": " + firstSelectId); //"First Select in div4: 4" 

イベントでは、あなたのdivは常にこれを行うことにより(addDropdownfield)あなたは自分の親の最後の子divを選択することができ、「DIV」で起動しません'id 'で相対参照が必要ない場合、' $('#div4 ')... 'を実行して具体的に選択することができます。 - しかし、あなたの質問では、選択しようとしていることが明確に定義されていませんどのような状況の下で。詳しく教えてください。 – Santi

+0

'$( '#div4')で直接divをターゲットできない理由はありますか? – PFlans

+0

おそらく' $(that).closest( '[id]')。attr( 'id') ' – Dimava

答えて

5

これは何か?あなたがいるなら

var lastDiv = $("#addDropdownfield div").last(); //Get all children divs of addDropdownfield - from that list, just the last one 
+0

@ Santi- great ...私のコードに入れます...しかし、もし私のIDが" div "で始まっていなかったら?どのようにそれを選ぶでしょうか?私はどのように私は最後のdiv idを任意の名前に属して選択することができますか? –

+0

あなたが探しているdivが常に 'addDropdownfield'の子になる場合は、$("#addDropdownfield div ")を実行することができます。last();' - '$("#addDropdownfield div ")'すべてのdivを選択します'addDropdownfield'の子で、' .last();最後のものを取得します。私は上記の私の答えを調整しました。 – Santi

+0

魅力的な兄弟のように働いていました... <3 <3 < –

関連する問題