如何删除动态添加的元素?

时间:2013-02-13 20:58:12

标签: javascript jquery html

我有这个代码,它在文本框的正下方添加了动态div,但是我想在用户删除字符后将其删除:

下面的代码不起作用,我看不出我做错了什么

Code from Fiddle

见下面的代码:

    $(document).ready(function () {

        $("#searchcontent").keypress(function (e) {
            var dvSearch = $('<div />');
            dvSearch.attr('dvSearch', '1');

            if ($("#searchcontent").val().length >= 2) {
                var pos = $(this).position();
                dvSearch.html("<div>ffff:<div><div>eeee:<div><div>ggggg:<div>").css({
                    top: pos.top + $(this).height() + 18,
                    left: pos.left,
                    width: '300px',
                    position: 'absolute',
                    'background-color': 'Yellow'
                }).insertAfter($(this));
            }
            else {
                $("div[dvSearch='1']").remove();
            }
        });

    });

4 个答案:

答案 0 :(得分:1)

我建议只准备一个容器来接收HTML中的结果内容。然后,您可以通过在那里添加结果或清空该容器来减少问题。在原始代码中,您不断在每个div事件中添加keypress个元素。

另外,正如其他人所提到的那样,您需要使用.keyup()代替.keypress()来确保在事件触发时输入的值是正确的。

<强> HTML

<input type="text" id="searchcontent" name="searchcontent" class="txtSearch" maxlength="30" />
<div id="searchresult"></div>

<强> JS

$(document).ready(function() {
    $("#searchcontent").keyup(function(e) {
        if ($("#searchcontent").val().length >= 2) {
            var pos = $(this).position();
            $("#searchresult").html("<div>ffff:<div><div>eeee:<div><div>ggggg:<div>");
        }
        else {
            $("#searchresult").empty();
        }
    });
});

jsfiddle

答案 1 :(得分:0)

问题是在实际值更新之前运行了按键。因此,如果文本框显示“hi”并按下退格键,则select的值仍为“hi”。

改变

$("#searchcontent").keypress(function(e){

$("#searchcontent").keyup(function(e){

它解决了这个问题。

Updated JS Fiddle

答案 2 :(得分:0)

问题是。按下退格时,.keypress()不会触发。改为使用.keyup()。

答案 3 :(得分:0)

总是为html运行选择器并不是一个好方法。尤为全球化。将链接保存到节点并通过它删除

$(document).ready(function () {
   $("#searchcontent").keyup(function (e) {
        var el = $(this),
            // getting link
            dvSearch  = el.data("searchBlock"),
            // getting passed state
            dvPassed = el.data("searchPassed"),
            pos;

        // setting if first run
        if (!dvSearch) {
            dvSearch = $("<div/>");
            // for what is it ?
            dvSearch.attr('dvSearch', '1'); 

            el.data("searchBlock", dvSearch);
            el.data("searchPassed", false);
        }




        if (el.val().length >= 2) {
            pos = el.position();

            // Inserting element if need
            if (!dvPassed) {
                dvSearch.insertAfter(el);
                el.data("searchPassed", true);
            }

            // setting html
            dvSearch.html("<div>ffff:<div><div>eeee:<div><div>ggggg:<div>").css({
                top: pos.top + el.height() + 18,
                left: pos.left,
                width: '300px',
                position: 'absolute',
                'background-color': 'Yellow'
            });
        }
        else {
            dvSearch.remove();
            el.data("searchPassed", false);
        }
    });

});