我正在进行类似于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
中只有1个被提交,它们使用AJAX
到POST
消息,但只是最后创建的作品。它们都是动态创建的,并分配了不同的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;
}
});
$('.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"});
}
}