2014-11-06 139 views
0

我一直在努力尋找解決方法,需要一些幫助。在子串中打印包含特定數字的數組項目的值

在我的網站上,每個用戶購物車都會有不同的物品,但是如果某個特定物品在購物車中,我希望將「刪除物品」按鈕複製到其他位置。

<!-- 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)); 
+0

請?您想在哪個活動中複製標籤?你如何區分哪個標籤被複制? – 2014-11-06 20:00:39

+0

@ user3491000我的答案能解決您的問題嗎? – elzi 2014-11-06 21:14:20

回答

0

下面介紹一種方法。不是我確定的最好方式。

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>

0
$(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) 
     } 

    }) 
} 
你能在更多的細節,你在做什麼解釋
相關問題