0

Possible Duplicate:
Triggering jquery event function at page load

I have a function to do something when a checkbox in a checkbox group is clicked to be checked. But now, I want something more. When the page is first time loaded, I have couple of the checkboxes pre-checked, so I want the function can be called for those checked checkbox, then, when user click checkbox, the same function will run again.

I tried .bind("ready, click",function())... doesn't work.

here is the current code which is working fine for click event, thanks!

<script>
$(document).ready(function(){

    $('.list_item').click(function(e){
        var facility_id = $(this).val();

        if ($(this).is(':checked')) {
                do somthing 1;
                    }
            });
            } else {
                do somthing 2;
                }
            }           
        });
    })
</script>
4

1 回答 1

0

Try this: using .each when DOM loads API = http://api.jquery.com/jQuery.each/

Further you can refactor like this: move .is(:checked) in seperate function and call it from click function.

The reason click work is because the click event is well bound to the element. but when you load .each will be the right API to use.

hope this helps, please let me know if I missed anything.

code

$(document).ready(function(){

    $('.list_item').each(function(e){
        var facility_id = $(this).val();

        if ($(this).is(':checked')) {
                do somthing 1;
                    }
            });
            } else {
                do somthing 2;
                }
            }           
        });
    })
于 2012-06-28T02:39:56.387 回答