Triggering jquery event function at page load

前端 未结 3 1737
醉梦人生
醉梦人生 2021-01-26 08:30

Here\'s a little jQuery function that does what I want when a checkbox is clicked, but I have a basic gap in my knowledge:

How to register this so that it deals with ch

相关标签:
3条回答
  • 2021-01-26 09:10

    like this?

    $(function() {
      $('fieldset input:checkbox').click(function() {
        if ($(this).attr('name') == 'foo') {
          // hide checkbox 'bar'
        }
        else {
          // show checkbox 'bar'
        }
      }).filter(":checked").click();
    })
    

    After seeing another answer i don't think the above is what you want. +1 to the other guy.

    :-)

    0 讨论(0)
  • 2021-01-26 09:13
    $(document).ready(function() {
       $('fieldset input:checkbox').click(handleClick)
         .filter(':checked').each(handleClick);
    });
    
    function handleClick(){
        if ($(this).attr('name') == 'foo') {
          // hide checkbox 'bar'
        }
        else {
          // show checkbox 'bar'
        }
    }
    

    You might need to add some logic in your handleClick function to make sure you know what state the checkbox is in. You can do this by calling $(this).is(':checked');

    0 讨论(0)
  • 2021-01-26 09:20

    You can put that code into a function and call the function immediately.

    For example:

    function handleCheckbox() {
        if ($(this).attr('name') == 'foo') {
          // hide checkbox 'bar'
        }
        else {
          // show checkbox 'bar'
        }
    }
    
    $(function() {
        $('fieldset :checkbox').each(handleCheckbox).click(handleCheckbox);
    });
    
    0 讨论(0)
提交回复
热议问题