创建一个选定单选按钮值的数组

时间:2019-02-11 09:38:46

标签: javascript jquery arrays

我有几个单选按钮:

<input type="radio" value="####.###/resources/videos/7.mp4">
<input type="radio" value="####.###/resources/videos/8.mp4">
<input type="radio" value="####.###/resources/videos/9.mp4">

如何制作一个包含所选值的数组,如下所示:

var videos = ["./resources/videos/7.mp4", 
             "./resources/videos/1.mp4", 
             "./resources/videos/2.mp4", 
             "./resources/videos/3.mp4"];

2 个答案:

答案 0 :(得分:0)

Onclick在数组中推送单选的值

var arr=[];
$('input').click(function(){
arr.push("."+$(this).val().split('####.###')[1])

console.log(arr);
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input type="radio" value="####.###/resources/videos/7.mp4">Video1
<input type="radio" value="####.###/resources/videos/8.mp4">Video2
<input type="radio" value="####.###/resources/videos/9.mp4">Video3

答案 1 :(得分:0)

我将添加一个public SimpsonVec SV_Setup(int index){ SimpsonVec sv; sv.A = points[index]; sv.B = points[index+1]; sv.C = points[index+2]; sv.D = points[index+3]; return sv; } public SimpsonVec[] SV_SETUP_ALL(){ SimpsonVec[] sv = new SimpsonVec[points.Length / 3]; for(int i = 0; i<points.Length / 3;i++){ sv[i] = SV_Setup(i*3); } return sv; } public Vector3 GetPoint (Vector3 p0, Vector3 p1, Vector3 p2, Vector3 p3, float t) { t = Mathf.Clamp01(t); float OneMinusT = 1f - t; return OneMinusT * OneMinusT * OneMinusT * p0 + 3f * OneMinusT * OneMinusT * t * p1 + 3f * OneMinusT * t * t * p2 + t * t * t * p3; } 事件侦听器,该事件侦听器将检查输入是否被选中或将其添加到列表中或从列表中删除。

change
var originalVideoList = ["./resources/videos/7.mp4",
  "./resources/videos/1.mp4",
  "./resources/videos/2.mp4",
  "./resources/videos/3.mp4"
];

var videos = document.querySelector("#videos");
var result = document.querySelector("#result");
var template = "<li><label for='{0}'>Video {1}</label><input id='{0}' type='checkbox' onchange='onChange()'/></li>";

var selectedArray = [];

// Set up html
videos.innerHTML = originalVideoList.map(function(video) {
  return template.replace(/\{0\}/g, video).replace(/\{1\}/g, video.split("/").pop());
}).join("");

// triggered on input change
function onChange() {
  selectedArray = toArray(document.querySelectorAll("li>input:checked")).map(function(item) {
    return item.id.replace("####.###", ".");
  });
  result.innerHTML = selectedArray.map(function(video) {
    return "<li>" + video + "</li>";
  }).join("");
}

// Same as [...input]
function toArray(input) {
  var result = [];
  for (var index = 0; index < input.length; index++) result[index] = input[index];
  return result;
}