The BootStrap iCheck plug-in Selects all and obtains the value.

Source: Internet
Author: User

The BootStrap iCheck plug-in Selects all and obtains the value.

When using the jQuery iCheck plug-in, we encountered a problem, that is, when we use the common JavaScript all-selection function, it is invalid.

$("#checkall").click(function(){if(this.checked){$("input[name='checkname']").each(function(){this.checked=true;});}else{$("input[name='checkname']").each(function(){this.checked=false;});}});

In this way, it is okay to write the default checkbox box, but it will be invalid when the iCheck plug-in is used.

How can this problem be solved?

The solution found in stackoverflow is as follows:

Address is here: http://stackoverflow.com/questions/17820080/function-select-all-and-icheck

// Obtain the value var checkAll = $ ('input. all '); var checkboxes = $ ('input. check'); checkAll. on ('ifchecked ifunchecked', function (event) {if (event. type = 'ifchecked') {checkboxes. iCheck ('check');} else {checkboxes. iCheck ('uncheck') ;}}); checkboxes. on ('ifchanged ', function (event) {if (checkboxes. filter (': checked '). length = checkboxes. length) {checkAll. prop ('checked', 'checked');} else {checkAll. removeProp ('checked');} checkAll. iCheck ('update ');});

After the full selection problem is solved, a new problem is encountered. When obtaining the value of the selected checkbox, use: $ (this ). attr ('checked'); the value cannot be obtained ~, It hurts.

In the last few Google searches, I found the inspiration in stackoverflow to determine the Boolean value of the checkbox, using: $ (this). is (': checked ');

The final solution of the Code is as follows:

$(".ajax-delete").click(function(){var url = $(this).attr('data-url');var str="";var ids="";$("input[name='id']:checkbox").each(function(){if(true == $(this).is(':checked')){str+=$(this).val()+",";}});if(str.substr(str.length-1)== ','){ids = str.substr(0,str.length-1);}console.log(ids);});

The above section describes how to select all the BootStrap iCheck plug-ins and how to obtain the value. I hope it will be helpful to you. If you have any questions, please leave a message for me, the editor will reply to you in a timely manner. Thank you very much for your support for the help House website!

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.