私がやろうとしていること:
jQueryを使用して、これらの新しく作成されたそれぞれの値を取得します。 <input>
これは私のHTML
Code
の影響を受ける部分です:<ul class="full_form_ul"> <li class="full_form_ul_li" id="the_step_li"> <button class="button" id="test_button">+[Test]</button> <button class="button" id="step_creation_button">+[Step]</button> </li> <!-- \\#the_step_li --> </ul> <!-- \\.full_form_ul -->
これは機能しているjQuery
code
です:$("#step_creation_button").click(function(event) { event.preventDefault(); $('#the_step_li').append('<div class="step_div"><input type="text" class="step_input"><button class="the_sub_step_button" type="button" title="button">+[Sub]</button></div>'); }); $('#the_step_li').on('click', "button", function(event) { event.preventDefault(); $(this).parent('.step_div').append('<div class="sub_step_div"><input type="text" class="sub_step_input"></div>'); });
動作し ないjQuery :
code
$("#test_button").on('click', function(event) { event.preventDefault(); $(".step_div").each(function() { alert($(this).next('.step_input').val()); // ^ updated according to @Jeremy T's excellent response into... //alert($(this).children('.sub_step_input').val()); $(this).children('.sub_step_div').each(function() { alert($(this).next('.sub_step_input').val()); // same change from .next into .children here as well I would assume, though it doesn't work in this instance. }); }); // \\.step_div.each //@EH_warch supplied a wonderful working solution for the initial problem as well //$("#the_step_li > .step_div > .step_input").each(function() //{ //alert($(this).val()); //}) }); // \\#test_button.on('click')
関連する(しかし異なる)質問: