3

我是第一次来这里,所以我为无意中违反规则而道歉。我正在尝试创建一系列按钮,如果单击这些按钮,则会将某些值添加到 url。这个想法是我想找出当人们点击最后的链接时需要传递 2 个变量中的哪一个。

这是我的代码:

 <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>

我认为我错过了一些愚蠢的东西,因为我的显示/隐藏功能不起作用,而且我认为链接创建不正确。想法?

4

1 回答 1

1

jsFiddle在这里。

<div id="Produc1">应该<div id="Product1">在您的 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");
  });
});
于 2013-05-26T16:05:48.840 回答