在一个网页,有一个类别列表,且每个类别标题的格式如下链接:的http://本地主机/管理/分类/取消发布/ 2
我写了下面的js code,试图捕捉URL和部分不发布(行动)和'2'(ID),并且需要将请求发送到 HTTP: //本地主机/管理/类别
$('#statusChanges实例一)。点击(函数(EVT){//使用超链接的点击事件
。EVT preventDefault();
VAR URL = $(位置).attr(HREF');
//变种行动= url.segment(3); / * JS控制台抱怨url.segment()方法的不确定! * /
//变种的id = url.segment(4);
$阿贾克斯({
键入:GET,
网址:$(位置).attr(HREF),
那:'',/ *我需要填充JSON数据中的数据:{动作:取消发布,ID:2}?但我不知道怎么弄段* /
成功:功能(数据){
$('。statusSuccess)文本(成功了!)。
},
错误:功能(数据){
$('。statusSuccess)文本(错误!)。
}
});
}); //状态变化的结束
解决方案
试试这个
VAR URL = $(位置).attr(HREF)分裂(/)拼接(0,5)。加入(/)。;
更新答:
用户这
对象来获取当前的锚链接见下文
$(本).attr(HREF)
On a webpage that has a list of categories, and each category title is linked in this format: http://localhost/admin/category/unpublish/2
I wrote the following js code, trying to capture the url and the segments 'unpublish' (action) and '2' (id), and need to send the request to 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
解决方案
Try this
var url = $(location).attr('href').split("/").splice(0, 5).join("/");
Update Answer:
User this
object to get current anchor link see below
$(this).attr('href')