我想写一些CoffeScript函数来检查表格中的所有复选框,当在th中选中复选框时。
我的CoffeScript函数如下:
$("table.tableview th input:checkbox").live 'click', ->
checkedStatus = this.checked
$("table.tableview tbody tr td:first-child input:checkbox").each ->
this.checked = checkedStatus
它很适合检查所有的框。但是当取消勾选时它不起作用。编译后的JS代码如下:
$("table.tableview th input:checkbox").live('click', function() {
var checkedStatus;
checkedStatus = this.checked;
return $("table.tableview tbody tr td:first-child input:checkbox").each(function() {
return this.checked = checkedStatus;
});
});
它不能工作,因为在第一个设置为false之后,函数的返回值将为false。然而,我不知道如何抑制coffee script的默认返回行为。请帮帮我。
当我按Flambino的建议添加“true”时,我得到以下JS
$("table.tableview th input:checkbox").live('click', function() {
var checkedStatus;
checkedStatus = this.checked;
$("table.tableview tbody tr td:first-child input:checkbox").each(function() {
return this.checked = checkedStatus;
});
return true;
});
我只能通过这种方式将函数内的return语句全部放到最后:
$("table.tableview tbody tr td:first-child input:checkbox").each ->
this.checked = checkedStatus
true
我做错了什么?非常感谢迄今为止提供的帮助。