admin管理员组

文章数量:1323714

I have 11 checkboxes with individual ids inside a modal popup.I want to have a hyperlink called SelectAll,by clicking on which every checkbox got checked.I want this to be done by javascript/jquery.

Please show me how to call the function

I have 11 checkboxes with individual ids inside a modal popup.I want to have a hyperlink called SelectAll,by clicking on which every checkbox got checked.I want this to be done by javascript/jquery.

Please show me how to call the function

Share Improve this question edited Jun 1, 2010 at 6:38 Sandipan asked Jun 1, 2010 at 6:30 SandipanSandipan 2192 gold badges5 silver badges11 bronze badges 1
  • And what did you try doing to achieve that? Do you have some js to show us that you'd like help with? – David Thomas Commented Jun 1, 2010 at 6:34
Add a ment  | 

5 Answers 5

Reset to default 8

You could attach to the click event of the anchor with an id selectall and then set the checked attribute of all checkboxes inside the modal:

$(function() {
    $('a#selectall').click(function() {
        $('#somecontainerdiv input:checkbox').attr('checked', 'checked');
        return false;
    });
});

You can do like this in jquery:

$(function(){
 $('#link_id').click(function(){
  $('input[type="checkbox"]').attr('checked', 'checked');
  return false;
 });
});

If you have more than one form, you can specify form id like this:

$(function(){
 $('#link_id').click(function(){
  $('#form_id input[type="checkbox"]').attr('checked', 'checked');
  return false;
 });
});

This should work, clicking on the element (typically an input, but if you want to use a link remember to also add 'return false;' to prevent the page reloading/moving) with the id of 'selectAllInputsButton' should apply the 'selected="selected"' attribute to all inputs (refine as necessary) with a class name of 'modalCheckboxes'.

This is un-tested, writing on my phone away from my desk, but I think it's functional, if not pretty.

$(document).ready(
  function(){
    $('#selectAllInputsButton').click(
      function(){
        $('input.modalCheckboxes').attr('selected','selected');
      }
    );
  }
);
$(function(){
    $('#link_id').click(function(e){
        e.preventDefault(); // unbind default click event
        $('#modalPopup').find(':checkbox').click(); // trigger click event on each checkbox
    });
});
  function CheckUncheck(obj) {
        var pnlPrivacySettings = document.getElementById('pnlPrivacySettings');
        var items = pnlPrivacySettings.getElementsByTagName('input');
        var btnObj = document.getElementById('hdnCheckUncheck');
        if (btnObj.value == '0') {
            for (i = 0; i < items.length; i++) {
                if (items[i].type == "checkbox") {
                    if (!items[i].checked) {
                        items[i].checked = true;
                    }
                }
            }
            btnObj.value = "1";
        }
        else {
            for (i = 0; i < items.length; i++) {
                if (items[i].type == "checkbox") {
                    if (items[i].checked) {
                        items[i].checked = false;
                    }
                }
            }
            btnObj.value = "0";
        }
  }

本文标签: javascriptHow to checkuncheck checkboxes by clicking a hyperlinkStack Overflow