[英]jquery make action forr element append
I am doing a chat, I want to confirm each message send or not? 我正在聊天,我想确认每个消息是否发送?
I do not know how to define the class confirm append, I want to make each class confirm to its answer 我不知道如何定义类确认附加,我想让每个类对其答案进行确认
$('#btn').click(function){
var message = $('textarea').val();
$('#chat').append(message+"<span class="confirm">Sending...</span>");
$.post('chat.php', function(result){
if($.trim(result)==='ok'){
this class confirm html('sent');
}else{
this class confirm html('error');
}
}
}
If I understand your syntax I believe you're looking for something like: 如果我了解您的语法,相信您正在寻找类似的东西:
$('#btn').click(function){
var message = $('textarea').val();
$('#chat').append("<p>"+message+" <span class='confirm'>Sending...</span><p>");
$.post('chat.php', function(result){
if($.trim(result)==='ok'){
$(".confirm").text('sent');
}else{
$(".confirm").text('error');
}
}
}
but I don't think your POST is going to work. 但我认为您的POST无法正常工作。 I assume you want to send the
message
to chat.php. 我假设您想将
message
到chat.php。 For that you'll need something more like: 为此,您需要更多类似的东西:
$.post( "chat.php", { message: message })
.done(function( result ) {
//
});
$("<div/>", {});
$("<div/>", {});
创建一个新元素(例如DIV $("<div/>", {});
and store it into a variable .addClass()
method to add the desired class to your memorized element .addClass()
方法将所需的类添加到您记忆的元素中 $('#btn').click(function() { var message = $('textarea').val(); // Create a new message element var $element = $("<div/>", { html: message + "<span class='confirm'>Sending...</span>", appendTo: "#chat" }); // Check response and add class $.post('chat.php', {data: message}, function(result) { $element.addClass( $.trim(result) === 'ok' ? 'sent' : 'error') }); });
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.