JQuery:选择动态创建的元素并推送到Firebase [英] JQuery: Selecting dynamically created elements and pushing to Firebase

查看:126
本文介绍了JQuery:选择动态创建的元素并推送到Firebase的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

初学者,所有这一切,玩弄Firebase。基本上,我想从Firebase中检索文本条目,并在其旁边有一个批准按钮。点击该按钮后,我希望将该特定文本条目推送到新的Firebase位置,并从页面中删除文本。我正在动态创建按钮和文本,并且在选择按钮和创建的div时遇到了一些麻烦。我知道我必须使用on(),但我不确定如何使用它。



谢谢!

< pre $ approveRef.on('child_added',function(snapshot){
var posts = snapshot.val();
$('< div id = ($('#feed'))。text(posts.text).append('< button style =buttonid =approve> Approve< / button>')。appendTo ;
}); $($#
$('#approve')。on(click,function(){
var text = $('#post')。val();
postsRef .push({'text':text});
$('#post')。remove();

});


解决方案

您必须绑定。 on()放在一个已经在页面上的动态添加元素的容器中,并且像这样:

<$ p $('#c $ c> $('#yourContainer')。on('click','#approve',function(){
//你的代码在这里..
});


Beginner to all of this, playing around with Firebase. Basically, I want to retrieve text entries from Firebase and have an "Approve" button next to it. When the button is clicked, I want that specific text entry to be pushed to a new Firebase location and the text removed from the page. I am creating the button and the text dynamically and I am having some trouble with selecting the button and the divs I created. I know I have to use on() but I'm unsure of how to use it.

Thanks!

approveRef.on('child_added', function(snapshot) {
 var posts = snapshot.val();
 $('<div id="post">').text(posts.text).append('<button style ="button" id="approve">Approve</button>').appendTo($('#feed'));
});

$('#approve').on("click", function(){
    var text = $('#post').val();
    postsRef.push({'text':text});
    $('#post').remove();

});

解决方案

You have to bind .on() on a container of your dynamically added element that is already on the page when you load it, and have it like this:

$('#yourContainer').on('click', '#approve', function(){
    //your code here..
});

这篇关于JQuery:选择动态创建的元素并推送到Firebase的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆