php - 这个 jQuery 选择器的问题

标签 php jquery html ajax

大家好,我有一个(可能)简单的问题,但我就是不明白为什么它不起作用。

下面的代码应该相当容易理解:

<?php
require('../db.php');
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
    "http://www.w3.org/TR/html4/strict.dtd"> 
<html lang="en"> 
<head> 

<meta http-equiv="content-type" content="text/html; charset=utf-8"> 
<title>Website Checker</title> 

<link href="style.css" rel="stylesheet" type="text/css" media="screen" />
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"></script>
<script type="text/javascript" src="js/jquery.simpletip-1.3.1.js"></script> 
<script type="text/javascript">
$(document).ready(function() {

$('#check').click(function(){

$('.http-status').html('<img src="images/spinner.gif"/>').fadeOut('slow').fadeIn('slow');

$('#site-list li.websites').each(function()
{
    //var newurl = $(this).find('span.url a').attr('href');
    var newurl = $(this).find('span.url').html();
    $.ajax(
    {
        type:    "POST",
        url:     "process.php",
        data:    ({"a":newurl}),
        cache:   false,
        success: function(message)
        {
            $(this).find('span.http-status').html(message);
        } //End AJAX return
    }); //End AJAX call
    }); //End li each
}); //End Check

$('#check').click();

});

</script>

<ul id="site-list" class="list"> 
<li class="title">
    <span class="id"></span>
    <span class="name">Title</span>
    <span class="url">URL</span>
    <span class="status">HTTP Status</span>
</li> 
<?php
// some PHP to fetch all the gig entries from the shows table
$sql = "SELECT * FROM `check`";
$query = mysql_query($sql) or die(mysql_error());
// a loop to place all the values in the appropriate table cells
while ($row = mysql_fetch_array($query)){
//begin the loop...
$id=$row['id'];
$name=$row['name'];
$url=$row['url'];
?>
<li class="websites">
    <span class="id"><?php echo $id; ?></span>
    <span class="name"><?php echo $name; ?></span> 
    <span class="url"><?php echo $url; ?></span>
    <span class="status http-status"></span>
</li>

<?php
}
?>
</ul>
<br /> 
<a href="#" id="check" class="button">Check Now</a>

这基本上从数据库中提取站点数据并将其呈现给用户,我想做的是当您单击要检查和显示的每个站点的http代码的按钮时(并且还首先在页面上运行)正在加载)

这似乎工作正常 - 发送和接收了正确的数据,但之后旋转器仍然在那里!

链接:http://www.4playtheband.co.uk/check/

process.php(如果有帮助):

<?php
require('../db.php');

$url= NULL;
if(isset($_POST['a'])) {    $url = mysql_real_escape_string($_POST['a']);   }

function Visit($url)
{
$agent = "Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)";$ch=curl_init();
curl_setopt ($ch, CURLOPT_URL,$url );
curl_setopt($ch, CURLOPT_USERAGENT, $agent);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch,CURLOPT_VERBOSE,false);
curl_setopt($ch, CURLOPT_TIMEOUT, 5);
$page=curl_exec($ch);

//echo curl_error($ch);
$httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);

if($httpcode>=200 && $httpcode<300) 
{
    echo '<span class="up">'.$httpcode.'<span class="icon"><img src="images/info.png" alt="website is up"/></span></span>'; exit;
}
else
{
    $httpcode=404;

    $date = date("l, j \of F Y \@ H:i");

    $to      = "someone@domain.com";
    $subject = "Urgent: $url is down";
    $message = "Hello,\n\nIt appears that on our latest check of $url on $date that the site was down.\n\nRegards,\nWeb Checker";
    $headers = 'From: noreply@webchecker.co.uk' . "\r\n" .
           'Reply-To: noreply@webchecker.co.uk' . "\r\n";

    mail($to, $subject, $message, $headers);
    echo '<span class="down">'.$httpcode.'<span class="icon"><img src="images/info.png" alt="website is down"/></span></span>'; exit;
}
}

if(!empty($url)){ Visit($url); exit; } 

?>

最佳答案

问题出在这里:

$(this).find('span.http-status').html(message);

根据 [jQuery .ajax 文档][1]:

The this reference within all callbacks is the object in the context option passed to $.ajax in the settings; if context is not specified, this is a reference to the Ajax settings themselves.

要解决此问题,您可以将 $( this ) 的值分配给另一个变量,并在 success 回调正文中访问该变量:

$('#site-list li.websites').each(function()
{
    theElement = $( this );
    var newurl = $(this).find('span.url').html();
    $.ajax(
    {
        // ...
        success: function(message)
        {
            theElement.html(message);
        } //End AJAX return

关于php - 这个 jQuery 选择器的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7285424/

相关文章:

javascript - JS 文字数组到 PHP

javascript - 如何将函数应用于具有同一类的多个元素?

javascript - 没有从 ajax 函数调用 PHP 脚本

php - Doctrine2 中的自定义集合

javascript - 当我使用 JQuery 创建文本框时,Materialise 日期选择器不起作用

JavaScript 无法与 API 通信

html - 在R中转换html西类牙语口音

html - 我怎样才能使引导下拉菜单向上而不是向下打开/展开

php - 如何在 Laravel 中为 JSON 字段添加数据库种子?

javascript - Jquery Validation 文件大小验证规则