简体   繁体   English

jQuery清除附加内容

[英]Jquery clearing appended content

I have an auto-complete text field for cities. 我有一个自动填写的城市文本字段。 For example cities are suggested in a dropdown list while you type. 例如,键入时建议在下拉列表中选择城市。 Upon selecting a city I use hide() to hide the text field and append the selected city name to a #city div, replacing the text field. 选择城市后,我使用hide()隐藏文本字段并将所选的城市名称附加到#city div上,以替换文本字段。

I also have an edit button that makes the text field reappear. 我还具有一个使文本字段重新出现的编辑按钮。 So when I click edit it should clear the previous appended city name and replace it with the new one I select. 因此,当我单击“编辑”时,它应清除之前附加的城市名称,并用我选择的新城市名称替换。

It's sort of like this. 有点像这样。 But I completely remove the text field http://www.emposha.com/demo/fcbkcomplete_2/ 但我完全删除了文本字段http://www.emposha.com/demo/fcbkcomplete_2/

$('#edit').click(function() {
              $(this).hide(); //hide edit button
              $("#city").append(""); //clear city name
              $("#city_text_field").show(); //show text field         
            });

When selecting a city again I use 当我再次选择城市时

$('#city').append('selected city');

this appends the new city by the previous one instead of replacing it. 这会将新城市附加到上一个城市,而不是替换它。 I also tried replaceWith() but nothing happens. 我也尝试过replaceWith()但是什么也没有发生。

Can anyone please suggest a Jquery function that is suitable for this? 任何人都可以建议适合此功能的Jquery函数吗?

You could first use 你可以先用

$("#city").empty();

or directly replace the contents with 或直接将内容替换为

$("#city").html('selected city');

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

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