简体   繁体   English

jQuery:将元素追加到inputfield并删除?

[英]jQuery: Append element to inputfield and remove?

I'm trying to append and remove element in the inputfield. 我正在尝试在输入字段中添加和删除元素。

I can simply append them and it works fine but I don't know hwy it doesn't get deleted/removed when I needed it to! 我可以简单地附加它们,并且效果很好,但是我不知道它在需要时不会被删除/删除!

To explain this issue I have created this fiddle: http://jsfiddle.net/hrL3gn1g/2/ 为了解释这个问题,我创建了这个小提琴: http : //jsfiddle.net/hrL3gn1g/2/

if you click on the images, it will append the element in the div Slevel as well as the inputfield. 如果单击图像,它将在div Slevel以及输入字段中附加该元素。

If you click on the elements inside the Div, it should delete/remove the element or string from inside the inputfield but it doesn't 如果单击Div内的元素,则应从输入字段内部delete/remove元素或字符串,但不会

could someone please advise on this issue? 有人可以建议这个问题吗?

You can try this: 您可以尝试以下方法:

$(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);

});

as i see , you try to remove the words(orange,apple etc) by using $(<input>).remove(<span>) , but you cant do this in that way.instead of this , i remove the selected <span> element. 如我所见,您尝试使用$(<input>).remove(<span>)删除单词(orange,apple等$(<input>).remove(<span>) ,但是您不能以这种方式这样做。相反,我删除了选择的<span>元素。

i changed a little the click listener function to this one: 我对此点击监听器功能做了一些更改:

//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();   
});

i changed the jsfiddle : http://jsfiddle.net/tornado1979/hrL3gn1g/3/ hope helps,good luck. 我更改了jsfiddle: http : //jsfiddle.net/tornado1979/hrL3gn1g/3/希望对您有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM