Javascript 单击href发送ajax帖子
单击链接时,我需要通过Javascript 单击href发送ajax帖子,javascript,jquery,joomla2.5,Javascript,Jquery,Joomla2.5,单击链接时,我需要通过ajax发送post。问题是,由于打开了链接,请求被丢弃 jQuery('#pagination a').click(function(){ jQuery.post('index.php?option=com_component', {checked_sites_for_session: jQuery('.selected-site input[type="checkbox"]').map(function () {return this.value
ajax
发送post
。问题是,由于打开了链接,请求被丢弃
jQuery('#pagination a').click(function(){
jQuery.post('index.php?option=com_component',
{checked_sites_for_session: jQuery('.selected-site input[type="checkbox"]').map(function () {return this.value;}).get()}
).success(function(){window.location = jQuery(this).attr('href')});
console.log(jQuery(this).attr('href'));
return false;
});
但同样的问题是(多亏了joomla)url中的问题——它类似于/~user/joomla/index.php/view sites?view=sites&page=2
——正如您看到的,它从斜杠开始,但真正的链接是http://localhost/~user/joomla/index.php/view sites?view=sites&page=2
,但在源代码中我对此有问题
jQuery('#pagination a').click(function(e){
e.preventDefault();
var me=this;
jQuery.post('index.php?option=com_component',
{checked_sites_for_session: jQuery('.selected-site input[type="checkbox"]').map(function () {return this.value;}).get()}
).success(function(){window.location = jQuery(this).attr('href')});
console.log(jQuery(me).attr('href'));
return false;
});
这有什么问题
jQuery('#pagination a').click(function(e){
e.preventDefault();
var me=this;
jQuery.post('index.php?option=com_component',
{checked_sites_for_session: jQuery('.selected-site input[type="checkbox"]').map(function () {return this.value;}).get()}
).success(function(){window.location = jQuery(this).attr('href')});
console.log(jQuery(me).attr('href'));
return false;
});
使用native element.href将获得包括域在内的整个href,而不仅仅是attributes值,而且在$,post函数中使用的
这个
关键字也存在范围问题:
$('#pagination a').on('click', function(e){
e.preventDefault();
var self = this;
$.post('index.php?option=com_component',
{checked_sites_for_session: $('.selected-site input[type="checkbox"]').map(function () {
return this.value;
}).get();
}
).success(function(){
window.location = self.href;
});
});
从您发布的链接来看,它们看起来根本不一样,因此您可能必须找到解决方案,因为某些CMS解决方案使用重写等。使用native元素。href将获得整个href,包括域,而不仅仅是属性值,在$,post函数中使用的
this
关键字也存在范围问题:
$('#pagination a').on('click', function(e){
e.preventDefault();
var self = this;
$.post('index.php?option=com_component',
{checked_sites_for_session: $('.selected-site input[type="checkbox"]').map(function () {
return this.value;
}).get();
}
).success(function(){
window.location = self.href;
});
});
从您发布的链接来看,它们看起来根本不一样,因此您可能必须找到解决方案,因为某些CMS解决方案使用重写等。
返回false
阻止默认操作并停止传播。真正的问题在于此
返回false
阻止默认操作并停止传播。真正的问题在于这个。