2013-03-21 107 views
0

這個咖啡腳本有什麼不對嗎?在這撥弄它的做工精細咖啡腳本語法問題

http://jsfiddle.net/Dtwigs/ThnKc/2/

但是當我嘗試將其轉換爲咖啡腳本它不希望似乎工作

$(document).on "change", "#print_size_options_LargeFormatBlackWhite", (event) -> 
    selected = undefined 
    index = undefined 
    selected = $(this).val() 
    index = $("select").index(this) 
    if selected is "customSize" 
    $($(".custom_size")[index]).fadeIn() 
    else 
    $($(".custom_size")[index]).fadeOut() 

回答

1

你的小提琴有幾個問題。

  1. 您對多個元素使用相同的ID - 您應該將其設爲一個類。

  2. 您正在將更改事件添加到文檔中,最好將其添加到更精確的選擇器,即選擇。

  3. 如果你打算用JS編寫它,那麼使用CoffeeScript轉換器沒有太大的收穫。

我已經解決了小提琴的一些問題並更新了它here

新的CoffeeScript看起來是這樣的:

$(".print_size_options_LargeFormatBlackWhite").change -> 
    selected = $(this).val() 
    index = $("select").index this 
    func = if selected is ".custom_size" then 'fadeIn' else 'fadeOut' 
    $($('.custom_size')[index])[func]() 
0

我能看到的唯一區別是,你寫(事件)這裏當小提琴是(evt)。

您是否在考慮使用http://js2coffee.org/進行轉換?

+0

他確實使用了js2coffee。 – phenomnomnominal 2013-03-21 19:32:41