我正在尝试做的事情:
使用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')
相关(但不同)的问题: