开发者

Jquery - change event firing on page load

开发者 https://www.devze.com 2022-12-19 19:09 出处:网络
I have select controls on a HTML page and using Jquery to set change event. But the event fires on the page load and does not fire when

I have select controls on a HTML page and using Jquery to set change event.

But the event fires on the page load and does not fire when I try to change values in the select controls.

Any i开发者_StackOverflow中文版deas why?

$(function() {
    $('select[id^=Start], select[id^=Finish], select[id^=Lunch]').change(CalculateHours($(this)));
});


Your syntax is wrong.

By writing change(CalculateHours($(this)), you are calling the CalculateHours function and passing its return value to jQuery's change method as if it were a function. You would only write this if your function returned another function and you wanted to addd the returned function as an event handler.

You need to write .change(function() { CalculateHours($(this)); })


You are attempting to bind the result of CalculateHours to tthe change event. Try this instead.

$(function() {
    $('select[id^=Start], select[id^=Finish], select[id^=Lunch]').change(function() { 
        CalculateHours($(this)); 
    });
});
0

精彩评论

暂无评论...
验证码 换一张
取 消