How does jquery get the values of multiple check boxes?

Source: Internet
Author: User

jquery traversal method can get the check box to the selected value

12 $("input:checkbox:checked").each(function(index,element));   // 为所有选中的复选框执行函数,函数体中可以取出每个复选框的值$("input:checkbox:checked").map(function(index,domElement)); // 将所有选中的复选框通过函数返回值生成新的jQuery 对象

Example Demo: Click the button to get the checkbox's selected value

  1. Creating HTML Elements

    12345678 <div class="box">    <span>点击按钮获取checkbox的选中值:</span>    <divclass="content">       <inputtype=‘checkbox‘name=‘message‘value=‘1‘/>发送短信       <input type=‘checkbox‘name=‘message‘value=‘2‘/>发送邮件    </div>    <input type="button"value="提交"></div>
  2. Set CSS Styles

    123 div.box{width:300px;padding:20px;margin:20px;border:4pxdashed#ccc;}div.box>span{color:#999;font-style:italic;}div.content{width:250px;margin:10px0;padding:20px;border:2pxsolid #ff6666;}
  3. Writing jquery Code

    12345678 $(function(){     $("input:button").click(function() {        text = $("input:checkbox[name=‘message‘]:checked").map(function(index,elem) {            return$(elem).val();        }).get().join(‘,‘);        alert("选中的checkbox的值为:"+text);    });});
  4. Observation effect

How does jquery get the values of multiple check boxes?

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.