在具有類別列表的網頁,每個類別標題鏈接的格式爲:http://localhost/admin/category/unpublish/2
使用jQuery獲取URL和提取URL段
我寫了下面的js代碼,試圖捕捉的網址和段 '不發佈'(動作)和 '2'(ID),並且需要將請求發送到http://localhost/admin/category
$('#statusChanges a').click(function(evt) { // use the click event of hyperlinks
evt.preventDefault();
var url = $(location).attr('href');
// var action = url.segment(3); /*JS console complains that url.segment() method undefined! */
// var id = url.segment(4);
$.ajax({
type: "GET",
url: $(location).attr('href'),
dat: '',
/* do I need to fill the data with json data: {"action": "unpublish, "id": 2 } ? but I don't know how to get the segments */
success: function(data) {
$('.statusSuccess').text('success!');
},
error: function(data) {
$('.statusSuccess').text('error!');
}
});
}); // end of status change
此問題可能會有幫助。 http://stackoverflow.com/questions/406192/how-to-get-the-current-url-in-javascript/16602140#16602140 – phpLearner