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

How to select multiple values ​​in checkbox in js?

不言
不言Original
2018-08-14 16:40:312091browse

The content of this article is about how to select multiple values ​​in the checkbox in js? It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

Idea: Use the name attribute value to obtain the checkbox object, and then loop to determine the checked attribute (true means selected, false means not selected). 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

Related recommendations:

What is implicit type conversion? Introduction to js implicit type conversion

#How to operate js to achieve the effect of clicking on a small picture to display a large picture? (Code example)

#What is the concept of js execution mechanism? Implementation method of js execution mechanism

The above is the detailed content of How to select multiple values ​​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