jquery - 如何使它成为for循环
问题描述
我认为它可以进入 for 循环,但我不知道如何表达它?它可以用我,但是如何表达#form+i,不管是#form[i]还是#form{i},我都试过了,还是不行。
另外,如果用户没有选择某个答案,我希望将“未定义”推送到数组中以避免其空白。
$('#form1').click(function() {
radioValue = $("input[name='answer1']:checked");
if (radioValue) {
arr.push(radioValue.val())
console.log(arr);
}
});
$('#form2').click(function() {
radioValue = $("input[name='answer2']:checked");
if (radioValue) {
arr.push(radioValue.val())
console.log(arr);
}
});
$('#form3').click(function() {
radioValue = $("input[name='answer3']:checked");
if (radioValue) {
arr.push(radioValue.val())
console.log(arr);
}
});
$('#form4').click(function() {
radioValue = $("input[name='answer4']:checked");
if (radioValue) {
arr.push(radioValue.val())
console.log(arr);
}
});
$('#form5').click(function() {
radioValue = $("input[name='answer5']:checked");
if (radioValue) {
arr.push(radioValue.val())
console.log(arr);
}
});
$('#form6').click(function() {
radioValue = $("input[name='answer6']:checked");
if (radioValue) {
arr.push(radioValue.val())
console.log(arr);
}
});
$('#form7').click(function() {
radioValue = $("input[name='answer7']:checked");
if (radioValue) {
arr.push(radioValue.val())
console.log(arr);
}
});
$('#form8').click(function() {
radioValue = $("input[name='answer8']:checked");
if (radioValue) {
arr.push(radioValue.val())
console.log(arr);
}
});
$('#form9').click(function() {
radioValue = $("input[name='answer9']:checked");
if (radioValue) {
arr.push(radioValue.val())
console.log(arr);
}
});
$('#form10').click(function() {
radioValue = $("input[name='answer10']:checked");
if (radioValue) {
arr.push(radioValue.val())
console.log(arr);
}
});
这是html的一部分。
<div class="stepShow">
<!--步驟區塊 -->
<div class="stepArea stepAreaActive">
<span><video style="width:300px" id="my_video" src="https://firebasestorage.googleapis.com/v0/b/capstone-daa95.appspot.com/o/real%2Ffake%2F001_870.mp4?alt=media&token=96b7884c-9367-48b9-b422-0e6c238570db"></video></span>
<form id="form1">
<p>Choose whether this video is fake or real?
<input name="answer1" type="radio" name="choice" value="real"> Real
<input name="answer1" type="radio" name="choice" value="fake"> Fake
</p>
</form>
<!-- <a class="pre" onclick="preFun(0)" href="javascript:;">Contact</a>
--> <a class="next" onclick="nextFun(0)" href="javascript:;">Next</a>
</div>
<div class="stepArea">
<span><video style="width:300px" id="my_video" src="https://firebasestorage.googleapis.com/v0/b/capstone-daa95.appspot.com/o/real%2Ffake%2F001_870.mp4?alt=media&token=96b7884c-9367-48b9-b422-0e6c238570db"></video></span>
<form id="form2">
<p>Choose whether this video is fake or real?
<input name="answer2" type="radio" name="choice" value="real" > Real
<input name="answer2" type="radio" name="choice" value="fake" > Fake
</p>
</form>
解决方案
我建议将相同的处理函数绑定到所有输入。
这有助于避免重复自己(参见DRY),并可能促进更可维护、更灵活和轻量级的代码。
这是一个例子:
change
将事件处理程序绑定到所有输入。- 当输入发生变化时,遍历所有步骤并找到它们检查的输入。
- 对于每个步骤,将其检查值(或未定义)添加到数组中。
const $steps = $('.stepArea');
const $inputs = $('.inptAnswer');
const $output = $('#output');
let stepValues = [];
function getStepValues() {
stepValues = $.map($steps, function(step) {
return $('.inptAnswer:checked', step).val() || [undefined];
});
$output.text(stepValues);
}
$inputs.on('change', getStepValues);
#output {
margin: 1em 0;
padding: 1em;
background: lightblue;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="stepShow">
<div class="stepArea stepAreaActive">
<form class="form">
Choose whether this video is fake or real?
<input class="inptAnswer" type="radio" name="choice" value="real"> Real
<input class="inptAnswer" type="radio" name="choice" value="fake"> Fake
</form>
</div>
<div class="stepArea">
<form class="form">
Choose whether this video is fake or real?
<input class="inptAnswer" type="radio" name="choice" value="real"> Real
<input class="inptAnswer" type="radio" name="choice" value="fake"> Fake
</form>
</div>
<div class="stepArea">
<form class="form">
Choose whether this video is fake or real?
<input class="inptAnswer" type="radio" name="choice" value="real"> Real
<input class="inptAnswer" type="radio" name="choice" value="fake"> Fake
</form>
</div>
<div class="stepArea">
<form class="form">
Choose whether this video is fake or real?
<input class="inptAnswer" type="radio" name="choice" value="real"> Real
<input class="inptAnswer" type="radio" name="choice" value="fake"> Fake
</form>
</div>
</div>
<div id="output"></div>
没有 jQuery,只是为了好玩:
const steps = document.querySelectorAll('.stepArea');
const inputs = document.querySelectorAll('.inptAnswer');
const output = document.getElementById('output');
let stepValues = [];
function getStepValues() {
stepValues = Array.from(steps).map((step) => {
let input = step.querySelector('.inptAnswer:checked');
return input ? input.value : undefined;
});
output.innerHTML = stepValues;
}
inputs.forEach((input) => {
input.addEventListener('change', getStepValues);
});
#output {
margin: 1em 0;
padding: 1em;
background: lightblue;
}
<div class="stepShow">
<div class="stepArea stepAreaActive">
<form class="form">
Choose whether this video is fake or real?
<input class="inptAnswer" type="radio" name="choice" value="real"> Real
<input class="inptAnswer" type="radio" name="choice" value="fake"> Fake
</form>
</div>
<div class="stepArea">
<form class="form">
Choose whether this video is fake or real?
<input class="inptAnswer" type="radio" name="choice" value="real"> Real
<input class="inptAnswer" type="radio" name="choice" value="fake"> Fake
</form>
</div>
<div class="stepArea">
<form class="form">
Choose whether this video is fake or real?
<input class="inptAnswer" type="radio" name="choice" value="real"> Real
<input class="inptAnswer" type="radio" name="choice" value="fake"> Fake
</form>
</div>
<div class="stepArea">
<form class="form">
Choose whether this video is fake or real?
<input class="inptAnswer" type="radio" name="choice" value="real"> Real
<input class="inptAnswer" type="radio" name="choice" value="fake"> Fake
</form>
</div>
</div>
<div id="output"></div>
推荐阅读
- java - 如何调用在主方法的许多子类中定义的一种方法 toString() 来显示多态性
- html - 响应式对齐图像
- python - 使用 keras VGGFace 框架训练 CNN 时,Epoch 未启动
- c# - 在 ASP.NET Core SignalR 中,如何将消息从服务器发送到客户端?
- python - python:为名称存储在变量中的变量赋值
- git - Git 错误致命:无法将 SSL 后端设置为“openssl”:已设置
- node.js - 使用 Node.JS 与 SOAP api 通信
- c# - 当我在 c# 中设置代理凭据时,为什么会收到“需要代理身份验证”
- php - 按字母顺序排列 Petfinder API 列表
- c# - 如何使贝塞尔曲线的单个锚点连续或不连续