[英]jQuery: Append element to inputfield and remove?
我正在尝试在输入字段中添加和删除元素。
我可以简单地附加它们,并且效果很好,但是我不知道它在需要时不会被删除/删除!
为了解释这个问题,我创建了这个小提琴: http : //jsfiddle.net/hrL3gn1g/2/
如果单击图像,它将在div Slevel
以及输入字段中附加该元素。
如果单击Div内的元素,则应从输入字段内部delete/remove
元素或字符串,但不会
有人可以建议这个问题吗?
您可以尝试以下方法:
$(document).on('click', '.pricetag',function(){
var names = $(this).attr('data-name');
var price = $(this).attr('data-price');
// Create the value you want to remove
var html = '<span data-price="'+price+'" data-name="'+names+'" class="pricetag">'+names+'</span>';
// Replace that value with empty string
var newValue = $('#Finalized').val().replace(html,'')
// Insert new value
$("#Finalized").val(newValue);
});
如我所见,您尝试使用$(<input>).remove(<span>)
删除单词(orange,apple等$(<input>).remove(<span>)
,但是您不能以这种方式这样做。相反,我删除了选择的<span>
元素。
我对此点击监听器功能做了一些更改:
//when the user click on a word
$(document).on('click', '.pricetag',function(){
//this is the <span> element that the user clicked to delete
$(this).remove();
});
我更改了jsfiddle: http : //jsfiddle.net/tornado1979/hrL3gn1g/3/希望对您有所帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.