我從兩個輸入創建兩個段落,但無法將輸入內容放入段落。從輸入創建具有動態ID和值的段落
下面是代碼:
HTML
<form id="ip-form" class="custom-form wow fadeInUp" data-wow-duration="1s" data-wow-delay="0.6s">
<div class="row">
<div class="col-6">
<div class="form-group">
<div class="input-group">
<input type="text" class="form-control custom-input-form" id="name" placeholder="Name">
</div>
</div>
</div>
<div class="col-6">
<div class="form-group">
<div class="input-group">
<input type="number" class="form-control my-ip" id="number" placeholder="Name">
</div>
</div>
<button type="submit" class="btn btn-gray no-side-margin float-right" id="add-new">Add</button>
</div>
</div>
</form>
<div class="row">
<div class="col-6" id="new-names">
</div>
<div class="col-6" id="new-numbers">
</div>
</div>
JS
var countName = 0;
var countNumber = 0;
$('#add-new').on('click', function(e) {
e.preventDefault();
$('#new-names').append('<p id="new-name-'+countName+'"></p>');
countName++;
$('#new-numbers').append('<p id="new-number-'+countNumber+'"></p>');
countNumber++;
$('#"new-name'+countName+'"').text($('#name').val());
$('#"new-number'+countNumber+'"').text($('#number').val());
});
我也簡單Codepen的代碼創建:https://codepen.io/fabiozanchi/pen/PJyeap
謝謝!
首先,建設'$( '# 「新名 '+ countName +'」')'被破壞,使用'$('#new-name'+ countName)' – dhilt