作为一个 jQuery 新手,我偶然发现了以下问题:
我有一个将字段集附加到现有表单的按钮。工作正常。每个字段集还有一个按钮,最终用于再次删除该确切字段。出于测试目的,我绑定了一个简单的 console.log 来单击任何删除按钮。
当我单击其中一个删除按钮时,文本会记录在控制台中。但是,它只会在所有先前附加的字段集从文档中完全消失之前闪烁一会。
这是html
<form id="theform" class="form-inline">
<fieldset class="aField" id="field1">
<select id="products">
<option>Item 1</option>
<option>Item 2</option>
<option>Item 3</option>
</select>
<input type="text" placeholder="Amount" class="amount">
<input type="text" placeholder="Price">
</fieldset>
</form>
<button id="addtoform" class="btn test">New Product</button>
这是js:
$(document).ready(function () {
$('#addtoform').click(function() {
var fields = $('.aField').length;
var newField = 'field' + String(fields + 1);
var button = "<button class='btn btn-danger delete'>delete</button>";
var newElem = $('#field1').clone().attr('id', newField).append(button);
$('#theform').append(newElem);
});
$("#theform").on("click", '.btn.btn-danger.delete', function () {
console.log('ok, let me delete this.');
});
});
怎么了?