问题描述
我在HTML页面上有选择控件,并使用Jquery来设置更改事件.
I have select controls on a HTML page and using Jquery toset change event.
但是该事件在页面加载时触发,在以下情况下不触发我尝试更改选择控件中的值.
But the event fires on the page load and does not fire whenI try to change values in the select controls.
有什么想法吗?
$(function() {
$('select[id^=Start], select[id^=Finish], select[id^=Lunch]').change(CalculateHours($(this)));
});
推荐答案
您的语法错误.
通过编写change(CalculateHours($(this))
,您正在调用 CalculateHours
函数,并将其返回值传递给jQuery的change
方法,就好像它是一个函数一样.仅当您的函数返回了另一个函数并且想将返回的函数添加为事件处理程序时,您才编写此代码.
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.
您需要写.change(function() { CalculateHours($(this)); })
这篇关于jQuery-更改页面加载时触发事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!