简体   繁体   中英

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. I assume you want to send the message to chat.php. For that you'll need something more like:

$.post( "chat.php", { message: message })
  .done(function( result ) {
    //
  });
  • Correct your syntax errors
  • Create a new element (like a DIV for example) by using $("<div/>", {}); and store it into a variable
  • After you get your PHP response use jQuery's .addClass() method to add the desired class to your memorized element

 $('#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') }); }); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM