[英]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.