事件处理程序无法使用jQuery
Event Handler not working with jQuery
我正在进行类似于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"});
}
}
相关文章:
- 用程序搜索JQuery数据表中的文本
- 页面加载之前的jQuery Ajax加载程序
- 使用jquery.load('pageName')方法时,未从应用程序缓存加载Html页
- 在web应用程序中的新搜索中重新加载搜索结果(不带jQuery)
- 滚动到Rails 4应用程序中的顶部jQuery
- 实现延迟的jquery更改事件处理程序
- 如何使jQuery的“bind”或“on”事件处理程序幂等
- Backbone listenTo不将jquery函数作为处理程序进行激发
- Jquery事件处理程序仅适用于匿名函数
- 应用程序在呈现Jquery Mobile之前显示无样式的Html
- 使用jquery删除输入框上的外部处理程序/库
- 如何在jQuery事件处理程序中存储和重用超时
- 访问离线jquery应用程序中的本地文件
- 我可以't使用jQuery更改工具提示引导程序的位置
- 为什么有些jQuery程序员使用像$.get、$.getJSON和$.when这样的jQuery函数来代替$.ajax
- jQuery,引导程序下拉列表问题
- jQuery$.ajax扩展程序XDomainRequest
- jQuery在页面加载时分配点击事件处理程序
- jQuery 事件处理程序堆叠
- jQuery程序不工作