jQuery从URL链接变量并通过Ajax发送 [英] jQuery to take url with variables from a link and send it via Ajax

查看:151
本文介绍了jQuery从URL链接变量并通过Ajax发送的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 < div id =add>我可以通过GET将产品添加到购物车中。 < a id =add-linkhref =http:// localhost / catalog / cart?product_id = 8& boutique_id [] = 36& func = Module :: Cart-> add>添加到购物车< / A>< / DIV> 

我想使用jQuery Ajax来保持同一页面(如果启用了JS)。
我列出了以下内容,但它当然不起作用。有人可以看看有什么不对吗?感谢。

 < script type =text / javascript> 
$(document).ready(function(){

$('a#add-link')。bind(click,function(event){
event .preventDefault();
var url = $(this).attr(href);
alert(现在我想调用这个页面:+ url);
$。 ajax({
type:GET,
url:url =+ url,
success:function(){
alert(Product added to cart);
}
});
});

});
< / script>


解决方案

 
var url = $ (本).attr(的 href);
alert(现在我想打电话给这个页面:+ url);
$ .get(url,function(resp){
alert(Product added to cart);
});


I can add a product to cart via GET with this link:

<div id="add"><a id="add-link" href="http://localhost/catalog/cart?product_id=8&boutique_id[]=36&func=Module::Cart->add">Add to Cart</a></div>

I want to use jQuery Ajax to stay on the same page (if JS is enabled). I have crapped out the following, but of course it does not work. Could someone please look what is wrong? Thanks.

<script type="text/javascript">
$(document).ready(function(){

$('a#add-link').bind("click", function(event) {
event.preventDefault();
var url = $(this).attr("href");
    alert("Now I want to call this page: " + url);
$.ajax({
  type: "GET",
  url: "url="+url, 
  success: function() {
    alert("Product added to cart");
  }
});
});

});
</script>

解决方案

var url = $(this).attr("href");
alert("Now I want to call this page: " + url);
$.get(url, function (resp) {
    alert("Product added to cart");
});

这篇关于jQuery从URL链接变量并通过Ajax发送的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆