2012-07-25 24 views
0

我有一个jQuery脚本,它将抓取一些html,并在每次单击“添加产品”时重复该脚本。我看起来很好,但我意识到可以添加多个产品(同一产品)。因此,单选按钮会发生干扰。他们需要独特的广播名称属性。我甚至不知道从哪里开始做这件事,我想我可以有一个计数器,并将其添加到当前的名称......预先感谢任何帮助。为单选按钮添加唯一名称

下面是HTML的例子:

<div id="product-container" class="product-container" style="display:none"> 
<div class="product-wrapper white-rounded"> 
    <div id="product-name" style="display:none"></div> 
     <h4>Example Product 1</h4> 
     <span class="align-right"> 
      <input name="select2" type="radio"><strong style="margin-right:20px">Option 1</strong> 
      <input name="select2" type="radio"><strong>Option 2</strong> 
     </span> 
     <div class="remove float-right"> 
      <a href="#"><div class="img-remove"></div>Remove</a> 
     </div> 
     <div class="clear"></div> 

    </div> 
</div> 

而且,这里是抓住代码并吐出它在一个锚点jQuery的:

$('.add-product').click(function() { 
     if (Products < 4 || (Products == 4 && Products2 == 1)) { 
     //Store html contents in Variable 
      var thisProduct = $('#product-container').html() 
     //Add account html to account center anchor point 
      $('.add-product').before(thisProduct) 
      $('#product-name').text(productName) 
     //Add one more Product Count 
      Products++ 
     //Add Product Count value to hidden Input Field, then manually trigger the change event. 
      $('.product-count').val(Products).change() 
     //Scroll to top of page after account is added 
      $('html, body').animate({scrollTop:0}, 'slow'); 
     } 

     else { 
      alert('HALP!'); 
     } 
    }); 
+0

它看起来像你的代码是重复的ID,以及与这一个不 - 不。 – j08691 2012-07-25 17:12:56

+0

好吧,它使用ID抓取容器内的html,并且不会在实际标记中重复它。至少我不这么认为......但我会仔细检查。谢谢。 – TurboSupra 2012-07-25 17:14:50

回答

1

你试图做名称的增量?

这样,它永远不会重复同一个名字!

+0

这正是我的想法,我只是没有超级棒的jQuery,我不确定如何做到这一点。 – TurboSupra 2012-07-25 17:16:35

0

最快的是解决这是只是有一个计数运行和追加,为您的ID或类,这样它会百达是唯一

var un1 = 0; 

for(yourloop){ 
    $("#whatever").append("idname"+un1); 
    un1++; 
} 
+0

这是有道理的,但我必须将'un1'附加到name =“whatever”字段。 我可能会解释这很可怕。当我多次添加这个html内容时,我选择一个单选按钮,它将取消选择所有其他单选按钮。所以,我需要更改名称字段以使它们都是唯一的。 – TurboSupra 2012-07-25 17:34:04