[英]How to loop over all the selected elements of an HTML page using jquery
I am working on a Quiz Application where I need to get all the selected elements or the user answers . 我正在做一个测验应用程序,我需要获取所有选定的元素或用户答案。 These elements can be radio input, check-box input or the text field.
这些元素可以是单选输入,复选框输入或文本字段。 every element is assigned a question_id attribute, answer_id and a mark attribute with it.
每个元素都分配有一个question_id属性,answer_id和一个mark属性。 What I want to do is I have to get these all question_id , answer_id and mark attribute so that I can calculate marks, and send the both question_id and answer_id to DB so that i can store the related user answer under a particular question.
我想做的是我必须获取所有的question_id,answer_id和mark属性,以便我可以计算分数,并将question_id和answer_id都发送到DB,以便我可以在特定问题下存储相关的用户答案。 i have rendered the quiz on template using this code.
我已经使用此代码对模板进行了测验。
$(data.quiztopics).each(function(index,element){
$(element.questions).each(function(index,question){
$(".quiz").append("<form name='question' class= question_"+question.id+"><input type='text' disabled value="+question.question_text+"/><br></form>");
if(question.question_type=='NUM'){
$(question.answers).each(function(index,answer){
$(".question_"+question.id).append("<input type='radio' question_id='+question.id+'answer_id='+answer.id +'name='answer' class=answer_"+answer.id+" mark="+answer.marks+"value="+answer.answer_text+">"+answer.answer_text+"</input>")
});
}
else if(question.question_type=='MCQ'){
$(question.answers).each(function(index,answer){
$(".question_"+question.id).append("<input type='checkbox' question_id='+question.id+'answer_id='+answer.id +' name='answer' class=answer_"+answer.id+">"+answer.answer_text+"</input>")
});
}
else if(question.question_type=='FIB'){
$(question.answers).each(function(index,answer){
$(".question_"+question.id).append("<input type='text' question_id='+question.id+'answer_id='+answer.id +' name='answer' class=answer_"+answer.id+">"+answer.answer_text+"</input>")
});
}
});
});
tell me how can i get the attributes of the selected elements for submitting the quiz. 告诉我如何获取所选元素的属性以提交测验。
I think this will do: 我认为这可以做到:
var questions = {};
$(".quiz :selected, .quiz :checked, .quiz input[type=text]").each({
var $this = $(this);
var question = $this.attr('question_id');
questions[question] = {
answer: $this.attr('class'),
};
});
its very simple, you just have to use element.attr( attributeName )
function 它非常简单,您只需要使用
element.attr( attributeName )
函数
A little JSFIddle to get you going 一点JSFIddle助您一臂之力
alert("Radio Mark " + $("#one").attr('mark') + ", Radio Value " + $("#one").attr('value'));
alert("check Mark " + $("#two").attr('mark') + ", check Value " + $("#two").attr('value'));
I have solved this problem by getting all the elements available in the DOM by their name, using getElementsByName('answer') method. 我已经通过使用getElementsByName('answer')方法按名称获取DOM中所有可用元素的方式解决了此问题。 It returns me a list then looping over this list i checked if the element is check or not if it is checked i get their attributes.
它返回给我一个列表,然后遍历此列表,我检查该元素是否被选中,如果被选中,则获取它们的属性。
attributes_list=new Array()
var answers=document.getElementsByName('answer');
for(i=0;i<answers.length;i++){
if(answers[i].checked){
question_id=answers[i].attributes['question_id'].value
answer_id=answers[i].attributes['answer_id'].value
attributes_list.push({'que':question_id,'ans':answer_id});
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.