简体   繁体   English

jQuery使动作forr元素追加

[英]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 ) {
    //
  });
  • Correct your syntax errors 更正语法错误
  • Create a new element (like a DIV for example) by using $("<div/>", {}); 通过使用$("<div/>", {});创建一个新元素(例如DIV $("<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 得到PHP响应后,使用jQuery的.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.

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