我想寫一個相當簡單的「全選」功能,但我得到的錯誤與我的JavaScript。該代碼是相當簡單的,所以我就張貼:'這'沒有被設置正確使用jQuery和CoffeeScript
(function() {
$(function() {
var all_check_box;
all_check_box = '#tournament_league_127';
return $(all_check_box).change(function() {
return $('.leagueCheckBox').each(function() {
return this.prop("checked", true);
});
});
});
}).call(this);
是由以下的CoffeeScript生成此代碼:
$ ->
all_check_box = '#tournament_league_127'
$(all_check_box).change ->
$('.leagueCheckBox').each ->
this.prop("checked", true)
然而,當我點擊#tournament_league_127,我得到以下錯誤:this.prop is not a function
。我不確定我做錯了什麼。任何幫助,將不勝感激。
我不明白......'this'和'$(this)'有什麼區別? – 2012-03-01 20:24:45
@Erica:'this'是事件處理程序綁定的元素。 '$(this)'用'this'(一個DOM元素)作爲參數調用jQuery並返回一個jQuery對象。 – 2012-03-01 20:26:22
你可以縮短它......'$('。leagueCheckBox')。prop(「checked」,true)' – 2012-03-01 20:29:09