我试图将当前选中的单选按钮推入我的阵列笔中。整个系列正在被推动。目前的选择应该是形状,大小,金属。我希望能够将它们连接成图像URL,但我一直在获取整个数组。我如何获得当前输入值?
<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");
}
});
答案 0 :(得分:1)
您需要添加索引pen.push(els[i]);
例如:
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");