繁体   English   中英

Javascript / HTML复选框获得检查值

[英]Javascript/HTML Checkboxes get checked value

我有几个复选框,每个复选框只有一个可选组件。 复选框的示例如下:

   <label class="checkbox inline">
        <input type="checkbox" id="question1option1" name="question1options" value="correct" onclick="SingleSelect('question1option',this)"> 1
    </label>
    <label class="checkbox inline">
        <input type="checkbox" id="question1option2" name="question1options" value="wrong" onclick="SingleSelect('question1option',this)"> 2
    </label>
    <label class="checkbox inline">
        <input type="checkbox" id="question1option3" name="question1options" value="wrong" onclick="SingleSelect('question1option',this)"> 3
    </label>
    <label class="checkbox inline">
        <input type="checkbox" id="question2option1" name="question2options" value="correct" onclick="SingleSelect('question2option',this)"> 1
    </label>
    <label class="checkbox inline">
        <input type="checkbox" id="question2option2" name="question2options" value="wrong" onclick="SingleSelect('question2option',this)"> 2
    </label>
    <label class="checkbox inline">
        <input type="checkbox" id="question2option3" name="question2options" value="wrong" onclick="SingleSelect('question2option',this)"> 3
    </label>

我有一个javascript函数,可以通过传递单选按钮组(如下所示)来拉出选中哪个单选按钮,是否有等效的人可以帮助我获取选中的复选框的值(每个“组”中只有一个)请类似的方式吗?

function getRadioValue (theRadioGroup)
    {
        for (var i = 0; i < document.getElementsByName(theRadioGroup).length; i++)
        {
            if (document.getElementsByName(theRadioGroup)[i].checked)
            {
                return document.getElementsByName(theRadioGroup)[i].value;
            }
        }
    }

提前致谢。

实际上,如果您更正对SingleSelect或标记的调用,则该功能将起作用。 您的标记使用的名称,如question1options但你要调用SingleSelect使用的是像名字question1option (不含s末)。 解决此问题并起作用: 实时示例

但是,显然,复选框在单击时不会自动取消选择具有相同名称的其他复选框(这就是type="radio"的意思)。 但是我想你知道。 :-)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM