2015-04-06 93 views
1

Folk,if if else in coffee script

我的html中有兩個文本框。我想用咖啡腳本比較他們的價值。儘管我已經搜索了這些內容,但我確信自己正在做預期的工作,但我仍然看到一種奇怪的行爲。

事情是:

比方說,我有一個id作爲「標題」和「作者」兩個文本框。隨着這一點,我有一個按鈕,onclick觸發咖啡腳本功能。

我的咖啡腳本函數看起來:

check_fields = -> 
    elem_book_title = $("#title").val() 
    elem_book_author = $("#author").val() 
    alert(elem_book_title) 
    alert(elem_book_author) 
    if not elem_book_title? 
    alert("title is null") 
    else if not elem_book_author? 
    alert("author is null") 
    else 
    alert("both are ok") 

的情況是,如果我只輸入在我的「標題」文本框的東西,它應該提醒我說:「作者是空」。對?但令人驚訝的是,它提醒我「兩個都沒問題」..預計?或者我錯過了什麼?

+2

不應該檢查值而不是元素? – Shomz 2015-04-06 11:05:34

+0

當你執行alert(elem_book_title.val())時,它會發出什麼警告? – 2015-04-06 11:06:59

+0

@Shomz,恩。是的,我想,太..喜歡的東西:。 'elem_book_title = $( 「#標題」)VAL()' 'elem_book_author = $( 「#作者」)VAL()' '警報( elem_book_title)' 'alert(elem_book_author)' 其餘代碼相同。但沒用。 – 2015-04-06 11:08:24

回答

5

在jQuery中,.val()將不會爲空字段(select元素除外)返回null。現在你的咖啡腳本計算結果爲:

if (elem_book_title == null) { 
    return alert("title is null"); 
} else if (elem_book_author == null) { 
    return alert("author is null"); 
} else { 
    return alert("both are ok"); 
} 

所以嘗試刪除問號

if not elem_book_title 
    alert("title is null") 
else if not elem_book_author 
    alert("author is null") 
else 
    alert("both are ok") 

會產生這樣的js我想你期待(這是falsiness像一個空字符串測試,0或null):

if (!elem_book_title) { 
    return alert("title is null"); 
} else if (!elem_book_author) { 
    return alert("author is null"); 
} else { 
    return alert("both are ok"); 
} 

有一個關於CoffeeScript的的方式存在操作問題(?)的作品here你可能會發現信息(謝謝@ raina77ow)。

+1

它不需要null來僞造。 – Shomz 2015-04-06 11:19:30

+0

我真的應該明確檢查長度嗎?我認爲必須有一些東西來避免這種情況..我想。 – 2015-04-06 11:20:34

+0

但看看你的coffeescript評估什麼:'if(elem_book_title == null)'。如果你刪除了問號,那麼它會測試虛假...我已經更新了我的答案。 – jcuenod 2015-04-06 11:21:42

0

您應該檢查"" (blank),因爲當文本框中沒有輸入值時,.val()函數將返回「」。

你可以做這樣的事情:

check_fields = -> 
    elem_book_title = "" 
    elem_book_author = "asdasd" 

    if not elem_book_title? or elem_book_title == "" 
    alert("title is null") 
    else if not elem_book_author? or elem_book_author == "" 
    alert("author is null") 
    else 
    alert("both are ok") 

生成JS

var check_fields; 

check_fields = function() { 
    var elem_book_author, elem_book_title; 
    elem_book_title = ""; 
    elem_book_author = "asdasd"; 
    if ((elem_book_title == null) || elem_book_title === "") { 
    return alert("title is null"); 
    } else if ((elem_book_author == null) || elem_book_author === "") { 
    return alert("author is null"); 
    } else { 
    return alert("both are ok"); 
    } 
}; 
+1

但是在CoffeeScript中,空字符串仍然是錯誤的(就像'null'和'undefined'),所以'!elem_book_title'就足夠了。 – 2015-04-06 16:55:47

-1

提示:在調試時,如果 - 不 - 其他 - 如果 - 不一樣,我覺得它更容易按照代碼使用,除非在塊中返回並返回

return alert("title is null") unless elem_book_title 
return alert("author is null") unless elem_book_author 
alert("both are ok") 

它產生s imilar JS

if (!elem_book_title) { 
    return alert("title is null"); 
} 
if (!elem_book_author) { 
    return alert("author is null"); 
} 
alert("both are ok");