1 1

Home >Web Front-end >JS Tutorial >How to get multiple values ​​selected in checkbox in js

How to get multiple values ​​selected in checkbox in js

一个新手
一个新手Original
2017-09-30 09:40:303148browse

Idea: Use the name attribute value to obtain the checkbox object, and then loop to determine the checked attribute (true means selected, false means unchecked). Here is an example demonstration:

1, HTML structure


<input type="checkbox" name="test" value="1" />
<span>
    1
</span>
<input type="checkbox" name="test" value="2" />
<span>
    2
</span>
<input type="checkbox" name="test" value="3" />
<span>
    3
</span>
<input type="checkbox" name="test" value="4" />
<span>
    4
</span>
<input type="checkbox" name="test" value="5" />
<span>
    5
</span>
<input type=&#39;button&#39; value=&#39;提交&#39; onclick="show()" />

2, javascript code (jQuery)


function show(){
    obj = document.getElementsByName("test");
    check_val = [];
    for(k in obj){
        if(obj[k].checked)
            check_val.push(obj[k].value);
    }
    alert(check_val);
}

3. Demonstration effect

The above is the detailed content of How to get multiple values ​​selected in checkbox in js. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn