通过js获取多个动态单选按钮值 [英] getting multiple dynamic radio button values through js

查看:472
本文介绍了通过js获取多个动态单选按钮值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的代码。我正在准备离线测验系统。此代码显示有选项字段的多个问题。现在在无线电变量中,目前它存储由用户提交的问题选项,其名称为q1。由于每个问题集的名称都是动态的,我如何在无线电变量中存储多个值?

类似于q1,q2等的选项...等等...

This is My Code. I'm preparing a offline quiz system. This code shows multiple questions with there option field. Now in radio variable, currently it stores the option of question submitted by user who's name is q1. Since the name of every question set is dynamic,how can I store multiple values in radio variable ?
Like option of q1, q2... and so on ...

var currentPath = ((location+"").replace(/%20/g, " ").replace("file:///", "").replace("/", "\\").replace("index.html", ""));
var pad = currentPath+"\\quiz.mdb";
//var strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + pad;
var strConn = "Provider=microsoft.ace.oledb.12.0;Data Source=" + pad;
var cn = new ActiveXObject("ADODB.Connection");
cn.Open(strConn);
var rs = new ActiveXObject("ADODB.Recordset");
var SQL = "SELECT * FROM ques_bank WHERE quizno = 'q_001'";
rs.Open(SQL, cn);
var sList = "<form target='sendinfo' id='infoform' onSubmit='return handleClick()'>";
while (!rs.EOF) {
    sList = sList +
    "<p>"+rs("question")+ "</p><br>"+
    "<input type='radio' name='q"+ rs("ID") +"' value='a' />"+rs("optionA")+"<br>"+
    "<input type='radio' name='q"+ rs("ID") +"' value='b' />"+rs("optionB")+"<br>"+
    "<input type='radio' name='q"+ rs("ID") +"' value='c' />"+rs("optionC")+"<br>"+
    "<input type='radio' name='q"+ rs("ID") +"' value='d' />"+rs("optionD")+"<br>"+
    "<input type='radio' name='q"+ rs("ID") +"' value='e' />"+rs("optionE")+"<br>"+
    "<input type='text' id='rank_list' name='q_id' value='"+ rs("ID") +"' /><br>"+
    "<hr>";
    rs.MoveNext();
}
document.write(sList+"<input type='submit' value='Submit'/></form>");

function test() {
}

//submit function
function handleClick() {
    var radios = document.getElementsByName("q1");
    var found = 1;
    for (var i = 0; i < radios.length; i++) {
        if (radios[i].checked) {
            alert(radios[i].value);
            found = 0;
            break;
        }
    }
    if(found == 1) {
        alert("Please Select Radio");
    }
    //event.preventDefault(); // disable normal form submit behavior
    return false; // prevent further bubbling of event
}

rs.Close();
cn.Close();

推荐答案

据我了解,点击提交按钮,你需要遍历所有 RadioButtons 并检查检查了哪些。



解决方案

As far as I understand, on "Submit Button" click, you need to loop through all the RadioButtons and check which ones are checked.

Solution
  1. 表单命名,如下所示。

  1. Give a name to the form like below.
<form name="form1" target="sendinfo" id="infoform" onsubmit="return handleClick()"></form>

  • 获取所有输入以下表格的元素。

  • Get all the input Elements of form like below.

    var inputs = form1.elements;



  • 仅查找 RadioButtons 来自这些元素。


  • Find only the RadioButtons from those Elements.

    var radios = [];
    
    //Loop and find only the Radios
    for (var i = 0; i < inputs.length; ++i) {
        if (inputs[i].type == 'radio') {
            radios.push(inputs[i]);
        }
    }



  • 使用无线电做任何你想做的事


  • 演示

    [演示]获取表单中的所有RadioButtons [ ^ ]



    注意

    在演示中,我已经注释掉了用于打破获取一个Radio值只是为了显示每个已检查的Radio值的警报。您可以根据您的逻辑和要求使用它。


    Demo
    [Demo] Get all RadioButtons inside the form[^]

    Note
    In the Demo, I have commented out the codes you used to break the on getting one Radio value just to show alert for every checked Radio value. You can use that as per your logic and requirements.


    这篇关于通过js获取多个动态单选按钮值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆