使用jQuery向URL添加变量
我是第一次来这里,所以我为无意中违反规则道歉。我正在尝试创建一系列按钮,如果单击这些按钮,将向url添加某些值。我的想法是,我想知道当用户点击最后的链接时,需要传递两个变量中的哪一个 这是我的密码:使用jQuery向URL添加变量,jquery,url,Jquery,Url,我是第一次来这里,所以我为无意中违反规则道歉。我正在尝试创建一系列按钮,如果单击这些按钮,将向url添加某些值。我的想法是,我想知道当用户点击最后的链接时,需要传递两个变量中的哪一个 这是我的密码: <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script> <script> jQuery(function(){ var product1
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script>
jQuery(function(){
var product1 = 'productId=1&productQuantity=1';
var product2 = 'productId=2&productQuantity=1';
var carturl = 'http://cart.net?clearCart=true';
jQuery(function(){
jQuery("#Product1Yes").click(function () {
jQuery("#Product2").show("fast");
jQuery("#Product1").hide("fast");
});
jQuery('a#upsell').attr('href', function() {
return carturl + '&' + product1;
});
jQuery("#Product1No").click(function () {
jQuery("#Product2").show("fast");
jQuery("#Product1").hide("fast");
});
jQuery("#Product2Yes").click(function () {
jQuery("#Product3").show("fast");
jQuery("#Product2").hide("fast");
});
jQuery('a#upsell').attr('href', function() {
return carturl + '&' + product2;
});
jQuery("#Product2No").click(function () {
jQuery("#Link).show("fast");
jQuery("#Product2").hide("fast");
});
});
});
</script>
<div id="Product1">
<button class="btn btn-primary" id="Product1Yes">Yes</button>
<button class="btn btn-danger" id="Product1No">No</button>
</div>
<div id="Product2">
<button class="btn btn-primary" id="Product2Yes">Yes</button>
<button class="btn btn-danger" id="Product2No">No</button>
</div>
<div id="Link">
<a id='upsell' href='#'>Click here to check out</a>
</div>
jQuery(函数(){
var product1='productId=1&productQuantity=1';
var product2='productId=2&productQuantity=1';
var carturl=http://cart.net?clearCart=true';
jQuery(函数(){
jQuery(“#Product1Yes”)。单击(函数(){
jQuery(“Product2”).show(“fast”);
jQuery(“Product1”).hide(“fast”);
});
jQuery('a#upsell').attr('href',function(){
返回carturl+'&'+product1;
});
jQuery(“#Product1No”)。单击(函数(){
jQuery(“Product2”).show(“fast”);
jQuery(“Product1”).hide(“fast”);
});
jQuery(“#Product2Yes”)。单击(函数(){
jQuery(“Product3”).show(“fast”);
jQuery(“Product2”).hide(“fast”);
});
jQuery('a#upsell').attr('href',function(){
返回carturl+'&'+product2;
});
jQuery(“#Product2No”)。单击(函数(){
jQuery(#Link).show(“快速”);
jQuery(“Product2”).hide(“fast”);
});
});
});
对
不
对
不
我想我遗漏了一些愚蠢的东西,因为我的显示/隐藏功能不起作用,而且我认为链接创建得不正确。想法?
应该是HTML中的
此外,您还缺少几个右大括号:
var product1 = 'productId=1&productQuantity=1';
var product2 = 'productId=2&productQuantity=1';
var carturl = 'http://cart.net?clearCart=true';
jQuery(function(){
jQuery("#Product1Yes").click(function () {
jQuery("#Product2").show("fast");
jQuery("#Product1").hide("fast");
});
jQuery('a#upsell').attr('href', function() {
return carturl + '&' + product1;
});
jQuery("#Product1No").click(function () {
jQuery("#Product2").show("fast");
jQuery("#Product1").hide("fast");
});
jQuery("#Product2Yes").click(function () {
jQuery("#Link").show("fast");
jQuery("#Product2").hide("fast");
});
jQuery('a#upsell').attr('href', function() {
return carturl + '&' + product2;
});
jQuery("#Product2No").click(function () {
jQuery("#Link").show("fast");
jQuery("#Product2").hide("fast");
});
});
@adeneo-这是在carurl中varaiable@Hogan-返回值将值返回给attr()方法,设置href。现在代码正在绑定单击内部单击等。您应该正确缩进代码,您会立即注意到类似的错误。
#Product3
不是defined@Hogan我猜他没有在HTML中创建这个部分。我现在已经添加了这个部分,但它似乎仍然不起作用。@Bertlymartizle Whe你添加了它吗?我在你的问题中没有看到它?显然它对Product3不起作用,因为你还没有定义它或给我它的代码,但是其他两部分工作如我的JSFIDLE中所示!我的错。我还在学习我应该在这些上做什么。谢谢你的帮助