2012-03-16 139 views
-3

有誰能告訴我爲什麼這個IF語句不起作用嗎?我使用jQuery和Firebug,但後者沒有給我任何有用的信息。IF語句的正確語法

我只是試圖揭示一個「提交」按鈕,當所有的領域已經完成,腳本運行每隔幾秒鐘來檢查輸入。

我的代碼摘錄去有點像這樣:

function checkForm(){ 
    var userName = $('#name').val(); 
    var userContent = $('#content').val(); 
    var userEmail = $('#email').val(); 

    // The following line shows me that the values for the fields are all getting picked up properly 
    $('#report').html("userName: "+userName+"<br />userContent: "+userContent+"<br />userEmail: "+userEmail); 

    // But the following line is throwing some kind of error 
    if (userName == "" || userContent == "" || userEmail == ""){ 
     $('#update').slideDown(); 
    } else { 
     $('#update').slideUp(); 
    } 
} 

$(document).ready(function(){ 

    $('#update').hide(); 
    setInterval('checkForm()' , 2000); 

}); 

而我的HTML ...

<div id="report"></div> 
<form id="submitfact"> 
    <div id="update">Update Database</div> 
    <label><input id="name" name="name" type="text" value="" /><span>Fact submitter's name</span></label> 
    <label><input id="email" name="email" type="text" value="" /><span>Fact submitter e-mail address</span></label> 
    <label class="content"><span>Fact text</span><br /><textarea id="content" name="content"></textarea></label> 
</form> 

編輯...

我道歉,如果人們認爲我通過不提供錯誤信息浪費他們的時間 - 但Firebug根本沒有給我任何有用的東西 - 如果是我會在這裏發佈它。我是一位經驗豐富的PHP程序員,但對jQuery相當新穎,所以我承認我仍然在掌握編寫語言和調試它。我想發佈Firebug響應的屏幕截圖,但作爲新用戶,我不被允許......我所得到的只是行號列中的「紅色錯誤圈/黃色玩三角形」圖標(「腳本「選項卡)上面顯示的行上......除非您可以告訴我除了」腳本「和」控制檯「面板以外還有什麼其他東西?

另一個編輯...

好吧,我把它固定,採取一看Cristoph的建議。它基本上是相同的解決方案,但不是將其稱爲函數,而是將其「內聯」。我不能完全肯定這兩種技術之間的區別是什麼,還是它只是我有一個地方的問題,但我的新的jQuery看起來是這樣的:

$(document).ready(function(){ 
    $('#submitfact').keyup(function(){ 
     var userName = $('#name').val(); 
     var userContent = $('#content').val(); 
     var userEmail = $('#email').val(); 

     $('#report').html(userName + "<br />" + userContent + "<br />" + userEmail); 

     if (userName == "" || userContent == "" || userEmail == ""){ 
      $('#update').slideUp(); 
     } else { 
      $('#update').slideDown(); 
     } 
    }); 
}); 

我會通過你的其他意見一看,看如果我可以簡化它,但至少我現在有一個工作基線!感謝您的時間,大家:)

+2

它們確實是空字符串,還是'null'?另外,當輸入被修改時,我會完成此檢查,而不是在定時器上。 – 2012-03-16 12:29:14

+0

如果某行發生錯誤,您是否認爲發佈錯誤是有益的?另外,Moo果汁是對的,你爲什麼要這樣做一個計時器?! – anothershrubery 2012-03-16 12:29:30

+0

**什麼是錯誤**? – gdoron 2012-03-16 12:31:37

回答

1

首先,是不是真的拋出一個錯誤,或者是它根本就沒有工作?

從我如何理解你的代碼,如果你的條件應該是:

if (!userName === "" && !userContent === "" && !userEmail === ""){ 
    // show 
    $('#update').slideDown(); 
} else { 
    // hide 
    $('#update').slideUp(); 
} 

二:用計時器做,這是一個壞主意。

介紹的事件處理程序檢查一次的改變inputValue的好遠:

$("input").change(function(){ 

    // if all inputs are filled, show Button, else hide it 

}); 

附: 對Javascript的深入瞭解:空字符串被視爲「falsy」,因此 username === ""可寫爲!username。但是請注意,undefined,null,false,0NaN也被認爲是「虛假」!這意味着,你無法區分它們。由於這個原因,我更喜歡username === ""note the === !

+0

謝謝Christoph - 簡單地改變我的代碼來匹配你的邏輯n第一個實例沒有什麼區別,但是重寫我的代碼作爲你的第二個建議做了訣竅。我已經相應地修改了我原來的職位。謝謝! :) – Doug 2012-03-16 15:17:45

+0

那麼它現在工作正常嗎? – Christoph 2012-03-16 15:20:45

+0

它是 - 我編輯了我的原始帖子以顯示正在運行的腳本...不確定是什麼導致了差異,TBH - 希望我能在jQuery變得更好時計算出來:) – Doug 2012-03-16 20:32:52

1

試着改變你的評價是:

if (!userName || !userContent || !userEmail){ 
    $('#update').slideDown(); 
} else { 
    $('#update').slideUp(); 
} 
+0

你可以用一點數學來進一步簡化它:'if(!(userName && userContent && userEmail)){' – Blazemonger 2012-03-16 12:39:14