我这几天一直在努力。这可行,但不会将正确的变量传递给页面(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>
答案 0 :(得分: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>
答案 1 :(得分:0)
实际上;您需要捕获click事件中的起始值和结束值,并选择所单击路径附近的起始值和结束值。
可能你想要的东西:
$(".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>