[英]Javascript Splice array value
Couple days ago I have asked a question, I will link it here, since it has some explanations about what I want, so you can get a idea what I want to do, JavaScript array splice This is solved now, and the codes works fine, But now I want to get the value of the card that has been spliced ( the cards we get when we drag them in box and click on it ) 几天前,我提出了一个问题,我将在此处链接它,因为它对我想要的内容进行了一些解释,因此您可以了解我想要做什么, JavaScript数组拼接,现在可以解决,并且代码工作正常,但是现在我想获得已拼接的卡片的价值(将它们拖入框中并单击后得到的卡片)
I thought this would be simple, but I guess I am wrong, 我以为这很简单,但我想我错了,
Concrete: I want that the user picks up 3 cards, and this will be mailed to the owner of the website, at the moment I can pick up 3 cards, by using splice the user can not pick multiple the same card, but now I want that the cards that it picks up, turn into a variable or something that I can mail them later. 具体:我希望用户拿起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>');
});
in the html> 在html>中
<script>
document.write ("Those will be mailed" + src[0] + "You get it ? "; )
</script>
JSFiddle : http://jsfiddle.net/arunpjohny/dkk2nqyg/9/ JSFiddle: http : //jsfiddle.net/arunpjohny/dkk2nqyg/9/
quite hard to explain, hope you get the idea. 很难解释,希望您能理解。
You can set the src value using data api while creating the elements 您可以在创建元素时使用数据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>');
});
then you can get an array of selected items using 那么您可以使用
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.