gpt4 book ai didi

javascript - 将当前元素插入我的数组

转载 作者:行者123 更新时间:2023-12-03 10:11:42 27 4
gpt4 key购买 nike

我正在尝试将当前选定的单选按钮插入我的数组“笔”中。整个系列都被推了。当前的选择应该是形状、尺寸、金属。我希望能够将它们连接到图像网址中,但我不断获取整个数组。如何获取当前输入值?

<h2>Pick a shape</h2>
<label>circle</label>
<input type="radio" name="shapes" value="circle" checked>
<input type="radio" name="shapes" value="square">
<label>square</label>
<input type="radio" name="shapes" value="heart">
<label>heart</label>
<br><br>

<hr>

<h2>Pick a metal</h2>
<input type="radio" name="metals" value="silver" checked>silver
<input type="radio" name="metals" value="bronze">bronze
<br><br>

<hr>

<h2>Pick a size</h2>
<input type="radio" name="size" value="sm">sm
<input type="radio" name="size" value="md" checked>md
<input type="radio" name="size" value="lg">lg

$('input:radio').change(function () {
var c = document.getElementById("controls");
var els = document.getElementsByTagName("input");
var pen = new Array();
//console.log(els);

for (var i = 0; i < els.length; i++) {
if (els[i].type == "radio" && els[i].checked == true) {
pen.push(els); // Should I add 'this'
console.log(pen);
}
console.log(pen[0] + "-" + pen[1] + "-" + pen[2] + ".png");
}
});

最佳答案

您需要添加索引pen.push(els[i]); ex:

for (var i = 0; i < els.length; i++) {    
if (els[i].type == "radio" && els[i].checked == true) {
pen.push(els[i]); // Should I add 'this'
console.log(pen);
}
}

console.log(pen[0] + "-" + pen[1] + "-" + pen[2] + ".png");

关于javascript - 将当前元素插入我的数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30080799/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com