jQuery:将元素追加到inputfield并移除

jQuery: Append element to inputfield and remove?

本文关键字:inputfield 追加 元素 jQuery      更新时间:2023-09-26

我正在尝试在inputfield中添加和删除元素。

我可以简单地附加它们,它工作得很好,但我不知道为什么它不会在我需要的时候被删除/删除!

为了解释这个问题,我制作了这个小提琴:http://jsfiddle.net/hrL3gn1g/2/

如果单击图像,它将在div Slevel中附加元素以及inputfield。

如果你点击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>)来删除单词(橙色、苹果等),但你不能这样做。我删除了所选的<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/希望有帮助,祝你好运。