2015-04-19 113 views
-2

我不知道如何可以改变的这本小册子中的内容,,如何通过JQuery更改<script>的内容?

<script id="pdis"> 
window.onload = function() { 
    var map = L.map('map').setView([-26.40894, -54.67430], 18); 

    L.tileLayer('http://{s}.tile.osm.org/{z}/{x}/{y}.png', { 
     attribution: '&copy; <a href="http://osm.org/copyright">OpenStreetMap</a> contributors' 
    }).addTo(map); 

     L.marker([-26.40893, -54.67438]).addTo(map) 
      .bindPopup("<b>eldorado punto de interes</b><br/> tu chichachicha.").openPopup(); 

     L.marker([-26.40896, -54.67403]).addTo(map) 
      .bindPopup("<b>Eldo punto 1</b><br />I am a popup.").openPopup(); 

}; 
</script> 

我试着搜索后更新这些L.markers,像

<?php 

include 'conexion.php'; 

$q=$_POST[q]; 
$con=conexion(); 

$sql="select * from pdi where name LIKE '".$q."%'"; 
$res=mysql_query($sql,$con); 

if(mysql_num_rows($res)==0){ 

echo '<b>No hay sugerencias</b>'; 

}else{ 

echo '<b>Sugerencias:</b><br/>'; 

while($fila=mysql_fetch_array($res)){ 

echo '<img src="logo/'.$fila['id'].'.jpg"></img><br><em>'.$fila['cat'].'</em><br><b>'.$fila['name'].'</b><br>'.$fila['dir'].'<br><a href="msite/'.$fila['id'].'.html" target="_blank">Más...</a>'; 
} 

} 

?> 

首先,我不知道如果我有使用JQuery的text()html()

+1

看起来好像您正在尝试在文档被发送到浏览器之前使用php *来更改html的内容。它是否正确?或者,你是否确实试图在将它发送到浏览器之后更改文档* – enigma

+0

如果您使用php使用它来生成具有正确标记的文档,则加载后无需在浏览器中进行更改。这是不可能的,但任何变量都可以被覆盖。 –

+0

@enigma传单地图的脚本位于城镇的中心,有一个标记开始,然后是一个搜索文本框,它发送匹配的php代码,并且我想用相同的mysql更新这些L.markers查询 –

回答

0

所以我解释你的问题的方式是,你想更新小册子地图上的标记与搜索返回的坐标。

为了做到这一点,您应该更改当前的代码,将标记存储在可访问的变量中。像

var markerA = L.marker([-26.40893, -54.67438]).addTo(map); 
var markerB = L.marker([-26.40896, -54.67403]).addTo(map); 

东西,你可以接着使用

markerA.setLatLng([newLat, newLong]) ; 

newLatnewLong需要通过你的PHP脚本Ajax调用返回改变标志的坐标。 JavaScript的这个看起来像

$.ajax({ 
    url: 'search.php', 
    data: {q: search_query}, 
    success: function(data){ 
    // update a single marker with the first set of returned coordinates 
    markerA.setLatLng([data[0].lat, data[0].long]) ; 
    }, 
    dataType: 'json' 
}); 

和PHP文件search.php看起来类似(从例如改变)

<?php 

include 'conexion.php'; 

$q=$_GET[q]; 
$con=conexion(); 

$sql="select * from pdi where name LIKE '".$q."%'"; 
$res=mysql_query($sql,$con); 

$result = array(); 

while($fila=mysql_fetch_array($res)){ 
    // append lat long coords to result array 
    $result[] = array('lat' => $fila['lat'], 'long' => $fila['long']); 
} 

echo json_encode($result); 

?> 

道歉,如果我犯了一个语法错误的任何地方 - 这是应该比实际代码更全面。

+0

你真是个好人,谢谢你给我新的想法!我不能upvote你的答案呢,但我很感谢 –

+0

@AndresBastias如果上面的帖子已经回答了你的问题,你可以通过点击投票按钮旁边的绿色标记来接受它,但是如果不是,请不要担心。享受编码! – enigma