jQuery:将元素追加到inputfield并移除
jQuery: Append element to inputfield and remove?
我正在尝试在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/希望有帮助,祝你好运。
相关文章:
- 表追加而不附加最后一个元素
- 如何在jquery中停止在更改时追加值
- JQuery.on(“keydown”)追加到页面时不工作
- jQuery[button.class]未检测到用按钮追加行
- 使用jQuery'生成输入字段;s追加
- js”;在“;不按特定顺序追加键/值
- 如何在 ReactJs 中追加到表
- 文本框仅接受十进制值,并且应在十进制后追加2个零
- 为什么DocumentFragment在追加后被清除
- 正在从JQuery追加Laravel Blade
- 有什么方法可以在下一个元素上进行追加吗
- jQuery-根据值在输入字段后追加图像
- jQuery动态使用.append()添加输入,但发布表单不是追加数据
- 追加Jquery后的Binding Click事件不起作用
- 包含数组的对象的QML ListModel追加中断
- JQuery追加到父级,只要它's已创建
- 一个接一个地追加两次
- 如何仅在数组没有的情况下追加到数组't包含带有rethinkDB的值
- Jasmine-无法将元素追加到DOM
- jQuery:将元素追加到inputfield并移除