我有一个方法,我想采取一些行动取决于复选框的'已检查'状态.'click'事件按预期工作,但foreach循环中的'myFunction'调用不会:
$(document).ready(function(){
$('#TreeView1 :checkbox')
.click(HandleCheckbox); // Works fine
// Go through the entire Category treeview to
// see if there are any checkboxes that are already
// checked.
$.fn.myFunction = HandleCheckbox;
$(":checked:checkbox").each(function(index) {
// In the HandleCheckbox 'this' is undefined.
$(this).myFunction();
});
});
function HandleCheckbox() {
alert(this.name + '=' + this.checked);
}
Run Code Online (Sandbox Code Playgroud)
在上面的代码中,当'.click'方法触发时,'this'被定义为,如预期的那样.当我在foreach循环中调用'muFunction'时,'this'在'HandleCheckbox'函数中未定义.
我仍在学习jQuery绳索,所以如果有更好或更简单的方法,请告诉我.
谢谢.
| 归档时间: |
|
| 查看次数: |
1552 次 |
| 最近记录: |