在jQuery Ajax之后执行功能吗?

Mar*_*per 0 javascript ajax jquery

我目前正在尝试让我的Ajax将一些信息从HTML表单发布到PHP表单中.

那么,现在我已经制作了差异代码,应该在"ajax.comment.php"页面打印出来,告诉发生了什么.

就像成功与否一样.

我现在想制作我的ajax,检查打印的html是什么.然后处理...就像if(打印HTML =="1"){然后做某事}.

我怎样才能做到这一点?

我的以下javascript是:

var name = document.comment.name.value;
var email = document.comment.email.value;
var website = document.comment.website.value;
var message = document.comment.message.value;
var id = document.comment.id.value;
$.ajax({
      type: "POST",
      url: "ajax.addcomment.php",
      data: "name="+name+"&email="+email+"&website="+website+"&message="+message+"&id="+id,
      beforeSend: function() {
        // Action before sending data
      },
      success: function(returned_html) {
        // Action after sending data, where the returned_html var is the returned html text by the php
      }
  });
Run Code Online (Sandbox Code Playgroud)

Ben*_*enM 6

在你的success功能中,试试这个:

success: function(returned_html) {
    var the_result = $.trim(returned_html);
    if(the_result == '1')
    {
        // Do whatever you wanted here.
    }
    else
    {
        // Do something else here...
    }
}
Run Code Online (Sandbox Code Playgroud)

  • 实际上,运行`$ .trim()`将**总是**返回一个字符串. (3认同)
  • 只是提到关于javascript的常用 - 你可能想做the_result ==='1'以确保结果是1和一个字符串 - 通常,如果你要做一些事情,最好早点明白the_result后来,它没有表现出你的期望 (2认同)