【问题标题】:Print value of array item containing specific number in a substring打印包含子字符串中特定数字的数组项的值
【发布时间】:2014-11-06 19:45:35
【问题描述】:

我已经尝试了几天来解决这个问题,需要一些帮助。

在我的网站上,每个用户的购物车中都会有不同的商品,但如果购物车中有特定商品,我希望将“删除商品”按钮复制到其他位置。

<!-- Here's how Business Catalyst outputs the buttons -->    
<div class="cart-item product-remove" style="border:solid 1px black;" >
  <div class="productitemcell"><a href="#" onclick="UpdateItemQuantity(0,324897,282161,9383682,336573,'','US');return false;">Remove Item</a></div>
  <div class="productitemcell"><a href="#" onclick="UpdateItemQuantity(0,324897,282161,9383705,336574,'','US');return false;">Remove Item</a></div>
  <!--the div below contains the button (anchor tag) I want to copy (It will move around the .product-remove div)-->
  <div class="productitemcell"><a href="#" onclick="UpdateItemQuantity(0,324897,282161,9383678,336585,'','US');return false;">Remove Item</a></div>
</div>    

<div id="sendLinkHere" style="border: 1px solid blue; width: 100%; height:50px;">
  <!-- I want the a tag containing "9383678" copied here -->
</div>    

这是我拥有的 javascript。我知道这不是很多,但这是我所拥有的。

var divsOfA = document.getElementsByClassName("productitemcell");

console.log($.inArray("9383678",divsOfA));

【问题讨论】:

  • 请您详细解释一下您在做什么?您想在哪个事件上复制 标签?如何区分要复制哪个 标签?
  • @user3491000 我的回答解决了你的问题吗?

标签: javascript arrays shopping-cart business-catalyst


【解决方案1】:

这是一种方法。我敢肯定这不是最好的方法。

var divsOfA = $(".productitemcell a");

$.each(divsOfA, function (i, obj) {

  var onclickVal = $(obj).attr('onclick');
  
  if ( onclickVal.indexOf('9383678') > 0 ) {
    
    var $parentCellClone = $(this).parent().clone();
    
    $('#sendLinkHere').append($parentCellClone);
  }
  
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<div class="cart-item product-remove" style="border:solid 1px black;" >
  <div class="productitemcell"><a href="#" onclick="UpdateItemQuantity(0,324897,282161,9383682,336573,'','US');return false;">Remove Item</a></div>
  <div class="productitemcell"><a href="#" onclick="UpdateItemQuantity(0,324897,282161,9383705,336574,'','US');return false;">Remove Item</a></div>
  <!--the div below contains the button (anchor tag) I want to copy (It will move around the .product-remove div)-->
  <div class="productitemcell"><a href="#" onclick="UpdateItemQuantity(0,324897,282161,9383678,336585,'','US');return false;">Remove Item</a></div>
</div>    

<div id="sendLinkHere" style="border: 1px solid blue; width: 100%; height:50px;">
  <!-- I want the a tag containing "9383678" copied here -->
</div>    

【讨论】:

    【解决方案2】:
    $(function() {
        copy(9383678);
    })
    
    function copy(num)
    {
        $('.productitemcell').each(function(){
            var $a=$(this).find('a');
            var str=$a.attr('onclick');
            if(str.indexOf(num)>-1)
            {
                $('#sendLinkHere').append($a)
            }
    
        })
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-11-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-11-18
      • 2019-12-07
      • 2021-10-23
      • 1970-01-01
      相关资源
      最近更新 更多