简体   繁体   English

我的代码有什么问题? (第二个on(单击)功能不起作用)

[英]What's wrong with my code? (second on(Click) function doesn't work)

I'm relatively new to Javascript so I might have made some bonehead mistake. 我是Java的新手,所以我可能犯了一些骨头错误。 I'm trying to code some simple javascript that removes a new list item. 我正在尝试编写一些简单的JavaScript来删除新的列表项。 The new list item itself was dynamically created when a different javascript function ran. 新的列表项本身是在运行其他javascript函数时动态创建的。 Here is the code: 这是代码:

$(document).ready(function () {
    str_to_append = '<li class="list-group-item"> <form method="POST" action="www.example.com" accept-charset="UTF-8" class="qty-form form-inline col-xs-9"><input name="_method" type="hidden" value="PATCH"><input name="_token" type="hidden" value="hglVSLGEqDVUpGw2RWq6qfAgcwzNg5vVquYRaevm"> <input class="col-xs-6" name="name" type="text" value=""> <span class="pull-right col-xs-6"> <input type="button" class="down col-xs-3" value="-" data-min="0"/> <input class="col-xs-3" maxlength="2" name="quantity" type="text" value="0"> <input type="button" class="up col-xs-3" value="+" data-max="50"/> <input type="submit" class="submit col-xs-3" value="&#x2705" name="submit"/> </span> </form> <span class="col-xs-3 delete"> <input type="submit" class="close-create col-xs-4" value="&#x2715" name="delete"/> </span> </li>';

    $(".addRow").on('click',function(){
        $("#foodItemRows").append(str_to_append);
    });

    $(".close-create").on('click',function(){
        alert("This worked?");
        //$("#new-create").remove();
    });
});

fiddle: http://jsfiddle.net/nobisnews/v0hpbtqj/1/ 小提琴: http : //jsfiddle.net/nobisnews/v0hpbtqj/1/

When I remove the first on('click',function(){....} it works. As in, 当我删除第一个on('click',function(){....}时,它可以正常工作。

$(document).ready(function () {
    str_to_append = '<li class="list-group-item"> <form method="POST" action="www.example.com" accept-charset="UTF-8" class="qty-form form-inline col-xs-9"><input name="_method" type="hidden" value="PATCH"><input name="_token" type="hidden" value="hglVSLGEqDVUpGw2RWq6qfAgcwzNg5vVquYRaevm"> <input class="col-xs-6" name="name" type="text" value=""> <span class="pull-right col-xs-6"> <input type="button" class="down col-xs-3" value="-" data-min="0"/> <input class="col-xs-3" maxlength="2" name="quantity" type="text" value="0"> <input type="button" class="up col-xs-3" value="+" data-max="50"/> <input type="submit" class="submit col-xs-3" value="&#x2705" name="submit"/> </span> </form> <span class="col-xs-3 delete"> <input type="submit" class="close-create col-xs-4" value="&#x2715" name="delete"/> </span> </li>';

    $("#foodItemRows").append(str_to_append);

    $(".close-create").on('click',function(){
        alert("This worked?");
        //$("#new-create").remove();
    });
});

clicking the x will create the alert. 单击x将创建警报。 However with the first on(click) function in there, it won't. 但是,有了第一个on(click)函数,就不会了。 Thanks. 谢谢。

You are listening for events on elements that have not been created yet. 您正在侦听尚未创建的元素上的事件。 Use event delegation instead: 改用事件委托:

$("#foodItemRows").on("click", ".close-create", function(){
    alert("This worked?");
    //$("#new-create").remove();
});

Just Replace the following code 只需替换以下代码

 $(".addRow").on('click',function(){
    $("#foodItemRows").append(str_to_append);
});

with the following 与以下

        $("#foodItemRows").append(str_to_append).on('click','.close-create',function()
        {
            alert('hello');
        })

You should add your event listener after you append the html, because the element does not exist yet. 附加html后,应添加事件侦听器,因为该元素尚不存在。

$(".addRow").on('click',function(){
        var a = $("#foodItemRows").append(str_to_append);
        a.find(".close-create").on('click', function(){
            alert("This worked?");
            //$("#new-create").remove();
        });
});

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

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