1

这几天我一直在为此苦苦挣扎。这可行,但不会将正确的变量传递给页面( testMap.php )。我使用数据库中的数据并将鼠标悬停在链接上确实会在 URL 中显示正确的变量,但无论出于何种原因,jquery 总是抓取循环中的第一个变量。有什么建议么?

<?php
    $myname = $_SESSION['username'];
    global $database;
    $stmt = $database->connection->query("SELECT * FROM ".TBL_FLIGHTS." WHERE username='$myname'");

        while ($row = $stmt->fetch(PDO::FETCH_ASSOC)){
            $date       = $row['date'];
                    $starting   = $row['starting'];
                    $ending     = $row['ending'];
                    $route      = $row['route'];
                  echo "<a class=\"route\" href=\"start=$starting&end=$ending\"><p class=\"pBlue\">$date - $starting - $ending - $route</p></a>";
                  ?>
        <div id="start" style="visibility:hidden"><?php echo $starting; ?></div>
        <div id="end" style="visibility:hidden"><?php echo $ending; ?></div>
        <?
    }

    ?>


<script type="text/javascript">
$(document).ready(function() {
    var start = $('#start').text();
    var end = $('#end').text();

    $(function() {
      $(".route").click(function(evt) {
         $("#mymap_canvas").load("testMap.php?start="+start+"&end="+end )
         evt.preventDefault();
      })
    })

});
</script>
4

2 回答 2

0

的确; 您需要在点击事件中捕获开始和结束值,并选择靠近被点击路线的开始和结束值。

可能是你想要的:

$(".route").click(function(evt) {
    var start = $(this).siblings(".start").text();
    var end = $(this).siblings(".end").text();
    $("#mymap_canvas").load("testMap.php?start="+start+"&end="+end )
    evt.preventDefault();
}); // I believe you are missing a semicolon here and at the end, too

但是,除非我遗漏了一些东西,否则你真的可以通过 php 构建那个 href(就像你已经用完整路径和查询字符串完成它一样),然后像这样执行它:

$(".route").click(function(evt) {
    // Load the href of the route anchor into 'mymap_canvas'
    $("#mymap_canvas").load($(this).attr("href"));
    event.preventDefault();
    return false; // Prevent the normal click behavior
});

没有更多隐藏的开始和结束!但请务必将您的原始锚标记更新为如下所示:

<a class=\"route\" href=\"testMap.php?start={$starting}&end={$ending}\">...</a>
于 2012-04-16T12:55:17.490 回答
0
<?php
    $myname = $_SESSION['username'];
    global $database;
    $stmt = $database->connection->query("SELECT * FROM ".TBL_FLIGHTS." WHERE username='$myname'");
$i=1;
        while ($row = $stmt->fetch(PDO::FETCH_ASSOC)){
            $date       = $row['date'];
                    $starting   = $row['starting'];
                    $ending     = $row['ending'];
                    $route      = $row['route'];
                  echo "<a class=\"route\" href=\"start=$starting&end=$ending\"><p class=\"pBlue\">$date - $starting - $ending - $route</p></a>";
                  ?>
        <div class="start<?php echo $i; ?>" style="visibility:hidden"><?php echo $starting; ?></div>
        <div class="end<?php echo $i ?>" style="visibility:hidden"><?php echo $ending; ?></div>
        <?
   $i++; }

    ?>


<script type="text/javascript">
$(document).ready(function() {
    var start = $('.start1').text();
    var end = $('.end<?php echo $i; ?>').text();

    $(function() {
      $(".route").click(function(evt) {
         $("#mymap_canvas").load("testMap.php?start="+start+"&end="+end )
         evt.preventDefault();
      })
    })

});
</script>
于 2012-04-16T12:38:36.393 回答