这几天我一直在为此苦苦挣扎。这可行,但不会将正确的变量传递给页面( 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>