2016-07-07 13 views
1

我正在輸出多個使用相同的<a>的動態鏈接。我的AJAX調用加載了所有鏈接的內容,但URL總是顯示第一個鏈接的標識。我怎樣才能讓它在點擊不同鏈接時更改網址中的ID?在URL中的Ajax調用data-id不發生變化後

$string .= '<a class="hrefid" data-id="'.$name["id"].'" href="#link">'.$name["name"].'</a>'. 
<div id="content"></div> 
$('.hrefid').on('click', function (e) { 
    var load = $(e.target).attr("href"); 
    if(load == "#link") { 
     $.ajax({ 
      type: 'post', 
      url: "/page/test/"+$(this).parents("[data-id]").attr("data-id"), 
      complete: function (event) { 
       $("#content").contents().remove(); 
       $("#content").append(event.responseText); 
       history.replaceState({}, "", "link"+$('[data-id]').first().attr('data-id')); 

      } 
     }); 
    } 
}); 
+0

這個問題可能是你使用history.replaceState:http://stackoverflow.com/questions/12832317/history-replacestate-example – awl19

回答

1

下面是解...

$('.hrefid').on('click', function (e) { 
var $this = $(this); 
var load = $(e.target).attr("href"); 
if(load == "#link") { 
    $.ajax({ 
     type: 'post', 
     url: "/page/test/"+id, 
     complete: function (event) { 
      $("#content").contents().remove(); 
      $("#content").append(event.responseText); 
      history.replaceState({}, "", "link" + $this.attr('data-id')); 
     } 
    }); 
} 
}); 
0

你要通過價值的jQuery的ID。將值傳遞給jquery後,你需要告訴url將傳輸數據的文件。

$('.hrefid').on('click', function (e) { 
    var load = $(e.target).attr("href"); 
    var id = $('#your input id').val(); 
    if(load == "#link") { 
     $.ajax({ 
      type: 'post', 
      url: "/page/test.php?action=update&id="+id, 
      complete: function (event) { 
       $("#content").contents().remove(); 
       $("#content").append(event.responseText); 
       history.replaceState({}, "", "link"+$('[data-id]').first().attr('data-id')); 

      } 
     }); 
    } 
}); 

上test.php的

if($_GET[action] == "update"){ 
    $id = $_GET['id']; 
    echo $id; 
} 
0

我認爲,你無法通過編程方式更改地址欄中的URL鏈接,導致這違反了安全規則。 想象一下,你打開一個不好的網站,它顯示你完全相同的頁面作爲你最喜歡的帳戶操作的銀行頁面,而且地址欄中的url鏈接與銀行的一樣...所以你不會懷疑這是不是真的銀行頁面。你在這個頁面上的行爲可能會導致你的證書被放棄。

您可以通過編程方式更改url鏈接的哈希部分。 這裏的維基關於hash fragment of url

相關問題