事件处理程序不适用于 jQuery

发布于 2025-01-05 01:50:02 字数 3359 浏览 1 评论 0原文

我正在进行类似于 Facebook 或 Gmail 的聊天,但有些东西不起作用。如果我打开了 5 个对话,则只有一个有效,只有一个显示/隐藏。您可以在 live-pin.com 中看到它。如果我粘贴一段代码是没有用的,因为它都涉及到,我需要粘贴我的所有网站!

但这里有一些聊天代码

function getOnJSON(){
    var from;var to;var msg_id;var msg_txt;var new_chat_string;

    //Getting the data from the json file
    $.getJSON("/ajax/end.emu.php",function(data){
    $.each(data.notif, function(i,data){
        from = data.from;to = data.to;msg_id = data.id;msg_txt = data.text;
        if ($("#chat_"+from+"").length === 0){
            $("#boxes").append('<div id="chat_'+from+'" class="chat_box hidden_box">'+
                '<div id="'+from+'_nick" class="chat_nick">'+from+'</div>'+
                '<ul id="'+from+'_txt" class="chat_txt">'+
                    '<li id="'+msg_id+'_txt_msg" class="chat_txt_msg">'+ msg_txt+'</li>'+
                '</ul>'+
                '<form class="new_message" method="POST" id="new_msg_'+from+'">'+
                    '<input type="text" placeholder="Enter your message..." id="'+from+'_input" class="new_input" name="post_text" />'+
                    '<input type="hidden" name="to" value="'+from+'" />'+
                        '</form>'+
                        '</div>');    
            $('#new_msg_'+from).submit(submitChatMsg);
            $('#'+from+'_txt').jScrollPane({stickToBottom: true});
            $('#'+from+'_nick').live("click", function(){ toggleChat('#chat_'+from); });
            // $('#boxes').delegate('.chat_nick', 'click', function() { toggleChat('#chat_'+this.id.replace('_nick', '')); });
            $('body').append('<embed src="http://cdn.live-pin.com/assets/pling.mp3" autostart="true" hidden="true" loop="false">');

        }else{
            var pane2api = $('#'+from+'_txt').data('jsp');
            var originalContent = pane2api.getContentPane().html();
            pane2api.getContentPane().append('<li id="'+msg_id+'_txt_msg" class="chat_txt_msg">'+ msg_txt+'</li>');
            pane2api.reinitialise();
                $('embed').remove();
            $('body').append('<embed src="http://cdn.live-pin.com/assets/pling.mp3" autostart="true" hidden="true" loop="false">');
        }
    });
    });
}

更新:修复了显示/隐藏问题。现在的主要问题是,仅提交了多个表单 .new_message 中的一种,它们使用 AJAXPOST 消息,但仅提交了最后创建的消息作品。它们都是动态创建的,并分配有不同的 ID。

$('.new_message').live('submit',function(){
    contactForm = $(this);
    valor = $(this + 'input:text').val();
    destinatary = $(this + 'input[type=hidden]').val();
    reponse_id = destinatary + "_input";
    if (!$(this + 'input:text').val()) {
        return false;
    } else {
        $.ajax({
            url: "/ajax/end.emu.php?ajax=true",
            type: contactForm.attr('method'),
            data: contactForm.serialize(),
            // success: submitFinished
            success: function(data){
                responsed = $.trim(data);
                if (responsed != "success") {
                    alert("An error occured while posting your message");
                }else{
                    $('#' + reponse_id).val("");
                }
            }
        });
        return false;
    }

});

I'm making a chat similar to the Facebook's or Gmail's one, but something is just not working. If I have 5 conversations opened, just one works and just one shows/hides. You can see it in live-pin.com. It would be useless if I paste a piece of code since it's all involved, I would need to paste all my site!

But here's a little bit of chat code

function getOnJSON(){
    var from;var to;var msg_id;var msg_txt;var new_chat_string;

    //Getting the data from the json file
    $.getJSON("/ajax/end.emu.php",function(data){
    $.each(data.notif, function(i,data){
        from = data.from;to = data.to;msg_id = data.id;msg_txt = data.text;
        if ($("#chat_"+from+"").length === 0){
            $("#boxes").append('<div id="chat_'+from+'" class="chat_box hidden_box">'+
                '<div id="'+from+'_nick" class="chat_nick">'+from+'</div>'+
                '<ul id="'+from+'_txt" class="chat_txt">'+
                    '<li id="'+msg_id+'_txt_msg" class="chat_txt_msg">'+ msg_txt+'</li>'+
                '</ul>'+
                '<form class="new_message" method="POST" id="new_msg_'+from+'">'+
                    '<input type="text" placeholder="Enter your message..." id="'+from+'_input" class="new_input" name="post_text" />'+
                    '<input type="hidden" name="to" value="'+from+'" />'+
                        '</form>'+
                        '</div>');    
            $('#new_msg_'+from).submit(submitChatMsg);
            $('#'+from+'_txt').jScrollPane({stickToBottom: true});
            $('#'+from+'_nick').live("click", function(){ toggleChat('#chat_'+from); });
            // $('#boxes').delegate('.chat_nick', 'click', function() { toggleChat('#chat_'+this.id.replace('_nick', '')); });
            $('body').append('<embed src="http://cdn.live-pin.com/assets/pling.mp3" autostart="true" hidden="true" loop="false">');

        }else{
            var pane2api = $('#'+from+'_txt').data('jsp');
            var originalContent = pane2api.getContentPane().html();
            pane2api.getContentPane().append('<li id="'+msg_id+'_txt_msg" class="chat_txt_msg">'+ msg_txt+'</li>');
            pane2api.reinitialise();
                $('embed').remove();
            $('body').append('<embed src="http://cdn.live-pin.com/assets/pling.mp3" autostart="true" hidden="true" loop="false">');
        }
    });
    });
}

UPDATE: Fixed the show/hide problem. Now the main problem is that just 1 of the multiple forms .new_message are submited, they use AJAX to POST the message but just the last created works. They are all created dynamically and have a different ID assigned.

$('.new_message').live('submit',function(){
    contactForm = $(this);
    valor = $(this + 'input:text').val();
    destinatary = $(this + 'input[type=hidden]').val();
    reponse_id = destinatary + "_input";
    if (!$(this + 'input:text').val()) {
        return false;
    } else {
        $.ajax({
            url: "/ajax/end.emu.php?ajax=true",
            type: contactForm.attr('method'),
            data: contactForm.serialize(),
            // success: submitFinished
            success: function(data){
                responsed = $.trim(data);
                if (responsed != "success") {
                    alert("An error occured while posting your message");
                }else{
                    $('#' + reponse_id).val("");
                }
            }
        });
        return false;
    }

});

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

倾城花音 2025-01-12 01:50:02
        $('.chatBox').live('click', function(e){ toggleChat($(this)); });
        function toggleChat(obj)
        {
            current_margin = obj.css('bottom');
            if (current_margin == "0px"){
                obj.animate({bottom : "-270px"});
            }
            else
            {
                obj.animate({bottom : "0px"});
            }
        }
        $('.chatBox').live('click', function(e){ toggleChat($(this)); });
        function toggleChat(obj)
        {
            current_margin = obj.css('bottom');
            if (current_margin == "0px"){
                obj.animate({bottom : "-270px"});
            }
            else
            {
                obj.animate({bottom : "0px"});
            }
        }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文