jqueryを使用して動的追加フォームを作成したいと思います。jqueryを使って要素を追加する方法
最後のテキスト入力の最後(3番目のテキスト入力)に注目すると、新しいdivを追加します。
JS & htmlファイル
$(document).ready(function() {
$("div.content:last span input.pdate").focus(function() {
var lastDiv = $(".content:last");
var newDiv = lastDiv.clone();
/* debugger;
num_of_product = num_of_product +1;
newDiv.find("#num_of_product").innerText=num_of_product;
console.log($(newDiv.find("#num_of_product").innerText)); */
lastDiv.after(newDiv);
})
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
<div class="header">
<label for="pname"> Product Name |</label>
<label for="pprice"> Product Price |</label>
<label for="pdate"> Product Date</label>
</div>
<div class="content" style="margin-bottom: 20px;">
<span id="num-of-product" style=" position: relative;left: -50px; top: 20px;"></span>
<span style="margin-left: 10px; position: relative;left: -50px; top: 20px;">
<input type="text" style="padding:2px; margin: 0 auto;width: 180px;height: 35px;" class="pname">
</span>
<span style="margin-left: 10px; position: relative;left: -1px; top: 20px;">
<input type="text" style="padding:2px;margin: 0 auto; width: 150px;height: 35px;" class="pprice">
</span>
<span style="margin-left: 10px; position: relative;left: 56px; top: 20px;">
<input type="text" style=" margin: 0 auto;padding:2px; width: 150px;height: 35px;" class="pdate">
</span>
<br class="clear">
</div>
出力
第1、第3要素(テキスト入力)に注目するとdivを追加します。私はに焦点を合わせた後、前の行から3番目の要素を追加したいと思います。
これに関するヘルプとヒントがあります。
'$( "div.content:最後のスパンinput.pdate")。フォーカス(...)は、' 1回だけ実行され、したがってフォーカスハンドラを持つ唯一の要素であります実行時の "最後の" 'div.content'要素です。 – Andreas