<a href="#?cat=MT&typ=1" data-reveal-id="reveal_popup" onclick="pop();" data-closeonbackgroundclick="false" data-dismissmodalclass="close-reveal-modal">due today</a>
<a href="#?cat=MT&typ=2" data-reveal-id="reveal_popup" onclick="pop();" data-closeonbackgroundclick="false" data-dismissmodalclass="close-reveal-modal">due in 2 days</a>
<div id="reveal_popup" class="reveal-modal">
<div id="popup"></div>
<a class="close-reveal-modal">×</a>
</div>
function pop() {
var cat=**value of cat parameter in href**
var type=**value of typ parameter in href**
$.ajax({
type:'post',
url:site_url()+'/event/deleteEventSingle',
data:{'cat':cat,'type':typ},
async:false,
success:function(result){}
});
}
此时,当用户点击href时,会出现相同的弹出窗口,其中包含不同的数据。实际上有超过10个hrefs,我试图在弹出窗口中显示用户输入数据的日历。这取决于两个参数 cat和typ ,如href。
所示每个href都有自己的cat和typ值。单击href时,我希望使用jquery获取所点击的href的cat和typ值,以便我可以在ajax中传递这些变量。
var cat=**value of cat parameter in href**
var type=**value of typ parameter in href**
How to get the parameters of an href attribute of a link from the click event object
答案 0 :(得分:11)
你可以做如下的事情
$('a').bind('click',function(){
var url = ($(this).attr('href'));
var cat = getURLParameter(url, 'cat');
var typ = getURLParameter(url, 'typ');
//calling the ajax function
pop(cat, typ)
});
function getURLParameter(url, name) {
return (RegExp(name + '=' + '(.+?)(&|$)').exec(url)||[,null])[1];
}
function pop(cat, typ) {
$.ajax({
type:'post',
url:site_url()+'/event/deleteEventSingle',
data:{'cat':cat,'type':typ},
async:false,
success:function(result){}
});
}
查看Live fiddle http://jsfiddle.net/mayooresan/aY9vy/
中的示例答案 1 :(得分:7)
试试这个:
function getUrlParams(url) {
var params = {};
url.substring(1).replace(/[?&]+([^=&]+)=([^&]*)/gi,
function (str, key, value) {
params[key] = value;
});
return params;
}
用法:的
function pop(e) {
var url = $(e.target).attr("href");
var params = getUrlParams(url);
var cat= params["cat"];
var type= params["typ"];
alert(cat);
alert(type);
}