gpt4 book ai didi

javascript - 如何使用jquery检查表单中多个动态单选按钮的值

转载 作者:行者123 更新时间:2023-11-28 14:47:39 24 4
gpt4 key购买 nike

我正在尝试制作一个测验系统。我创建了动态单选按钮。现在我想检查所选单选按钮的值。我怎样才能做到这一点?

 function createquestionpaper(qlist)
{
var questionform = $('#quesform');
var quesNum = 1;
$.each(qlist, function (index, val) {
var questionstring = '<div class="form-group" ><label class="control-label" id="'+val.Question_ID+'">'+quesNum+'. '+val.Question1+'</label>'+
'<div class="radio"><input type="radio" name="'+val.Question_ID+'" value="1">'+val.Option1+'</div>' +
'<div class="radio"><input type="radio" name="' + val.Question_ID + '" value="2">' + val.Option2 + '</div>' +
'<div class="radio"><input type="radio" name="' + val.Question_ID + '" value="3">' + val.Option3 + '</div>' +
'<div class="radio"><input type="radio" name="' + val.Question_ID + '" value="4">' + val.Option4 + '</div>' +
'</div><hr>';
questionform.append(questionstring);
quesNum = quesNum + 1;

});

最佳答案

因为其他答案没有提到同时检查所有值的解决方案:

// right answers
var solutions = [1,3,1,1,2,3,4];
// all answers from all questions (get all checked radio-inputs, then get all values)
var valuesForAllQuestions = $('.form-group', '#quesform').find('input:radio:checked').map(function() {return $(this).val()}).get();
// map them to points (1=correct answer, 0=false), then reduce it to a sum
var correct = valuesForAllQuestions.map((answer, idx)=>(answer==solutions[idx]?1:0)).reduce((sum, actual)=>sum+actual,0);

关于javascript - 如何使用jquery检查表单中多个动态单选按钮的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45848618/

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