在jQuery .load Ajax之后执行if语句

时间:2022-11-11 18:22:09

I'm using jQuery .load() to get info out of a database. Before loading the info into a div i would like to check the content with a if statement.

我正在使用jQuery .load()从数据库中获取信息。在将信息加载到div之前,我想用if语句检查内容。

For example: (the ajax will return 1)

例如:( ajax将返回1)

$('div.status').load("../files/ajax-calls.php?type=validatePerformer&performerId=265486");

if($('div.status').text() == 1){
  alert("ajax input checked, value = 1");
}else{
  return false;
}

i don't know if it's possible to check incoming ajax variables this way because jQuery write these directly into the DOM.

我不知道是否有可能以这种方式检查传入的ajax变量,因为jQuery直接将这些变量写入DOM。

The solution i'm seeking would look like this (i know this isn't correct coding, just to give an idea)

我正在寻找的解决方案看起来像这样(我知道这不是正确的编码,只是为了给出一个想法)

   var ajaxResult = $('div.status').load("../files/ajax-calls.php?type=validatePerformer&performerId=265486");

    if($(ajaxResult) == 1){
      $('div.status').text(ajaxResult);
    }else{
      return false;
    }

Thanks in advance!

提前致谢!

1 个解决方案

#1


1  

You can pass a callback to the .load function...

您可以将回调传递给.load函数...

$('div.status')
    .load("../files/ajax-calls.php?type=validatePerformer&performerId=265486",
          function() { alert("complete"); })

...but it won't prevent jQuery from inserting the new content, since this is done before the callback occurs.

...但它不会阻止jQuery插入新内容,因为这是在回调发生之前完成的。


To manually insert based on a condition, you should probably use $.get instead.

要根据条件手动插入,您应该使用$ .get代替。

$.get("../files/ajax-calls.php?type=validatePerformer&performerId=265486",
      function(ajaxResult) {
          if(ajaxResult == 1)
             $('div.status').text(ajaxResult);
      });

#1


1  

You can pass a callback to the .load function...

您可以将回调传递给.load函数...

$('div.status')
    .load("../files/ajax-calls.php?type=validatePerformer&performerId=265486",
          function() { alert("complete"); })

...but it won't prevent jQuery from inserting the new content, since this is done before the callback occurs.

...但它不会阻止jQuery插入新内容,因为这是在回调发生之前完成的。


To manually insert based on a condition, you should probably use $.get instead.

要根据条件手动插入,您应该使用$ .get代替。

$.get("../files/ajax-calls.php?type=validatePerformer&performerId=265486",
      function(ajaxResult) {
          if(ajaxResult == 1)
             $('div.status').text(ajaxResult);
      });