簡體   English   中英

Javascript Splice數組值

[英]Javascript Splice array value

幾天前,我提出了一個問題,我將在此處鏈接它,因為它對我想要的內容進行了一些解釋,因此您可以了解我想要做什么, JavaScript數組拼接,現在可以解決,並且代碼工作正常,但是現在我想獲得已拼接的卡片的價值(將它們拖入框中並單擊后得到的卡片)

我以為這很簡單,但我想我錯了,

具體:我希望用戶拿起3張卡,並將其郵寄到網站的所有者,此刻我可以拿起3張卡,通過使用拼接,用戶不能選擇多張相同的卡,但是現在我希望它撿起的卡片,變成一個變量或以后可以郵寄的東西。

$(function () {
var cars = ["1", "2", "3", "4", "5", "6", "7", "8", "9", "10"];
 var rand = cars[Math.floor(Math.random() * cars.length)];
$(".cards img").each(function (index) {
var src = cars.splice(Math.floor(Math.random() * cars.length), 1);
console.log(src, cars.join())
 $(this).wrap('<div class="front"></div>')
.parent().wrap('<div class="flipper"></div>')
.parent().wrap('<div class="flip-container"></div>')
.append('<div class="back"><img src="kaart/'+ src[0] + '.png"</img> </div>');
    });

在html>中

<script> 
 document.write ("Those will be mailed" + src[0] + "You get it ? "; ) 
</script>

JSFiddle: http : //jsfiddle.net/arunpjohny/dkk2nqyg/9/

很難解釋,希望您能理解。

您可以在創建元素時使用數據API設置src值

$(".cards img").each(function (index) {
    var src = cars.splice(Math.floor(Math.random() * cars.length), 1);
    $(this).wrap('<div class="front"></div>')
    .parent().wrap('<div class="flipper"></div>')
    .parent().wrap('<div class="flip-container"></div>')
    .data('src', src[0])
    .append('<div class="back"><img src="' + src[0] + '.png"</img> </div>');
});

那么您可以使用

    var selected = $('#dvDest .flipper').map(function(){
        return $(this).data('src')
    }).get();
    //can use ajax to sent this data to server and then mail it

演示: 小提琴

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM