【发布时间】:2021-02-07 03:17:21
【问题描述】:
我发现此代码非常有问题,因为它不会仅编辑单个列表项(单击的项),而是一次将所有列表项编辑到相同的输入。任何建议表示赞赏。小提琴链接如下:
https://jsfiddle.net/hufflepuff_hamlet/hy564btq/8/
<div>
<form name="checkListForm" class="edit-form">
<input type="text" name="checkListItem"/>
</form>
<div id="button1" value="button">Add!</div>
<div id="button2" value="button">Edit!</div>
<p></p>
<div class="list" id="item"></div>
<p>[Click item on list to edit item]</p>
<p>[Doubleclick item on list to remove item]</p>
</div>
</div>
//adding an item to the list
$(document).ready(function(){
$('#button1').click(function(){
var toAdd = $('input[name=checkListItem]').val();
$('.list').append("<div class='item'>" + toAdd + "</div>" + "<br>");
});
$('.list').click(function(){
});
//removing an item from the list by double-clicking on it
$(document).on('dblclick', '.item', function(){
$(this).remove();
});
//editing an item from the list by clicking on it and the edit button
$(document).on('click', '.item', function(){
var toAdd = $('input[name=checkListItem]').val();
$(this).text();
$(input).text(content);
$(this).remove();
$('#button2').click(function(){
var toAdd = $('input[name=checkListItem]').val();
$('.item').replaceWith("<div class='edit-form'>" + toAdd + "</div>" + "<br>");
});
$('.item').click(function(){
});
});
});
【问题讨论】:
标签: javascript jquery forms button