有誰能告訴我爲什麼這個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();
}
});
});
我會通過你的其他意見一看,看如果我可以簡化它,但至少我現在有一個工作基線!感謝您的時間,大家:)
它們確實是空字符串,還是'null'?另外,當輸入被修改時,我會完成此檢查,而不是在定時器上。 – 2012-03-16 12:29:14
如果某行發生錯誤,您是否認爲發佈錯誤是有益的?另外,Moo果汁是對的,你爲什麼要這樣做一個計時器?! – anothershrubery 2012-03-16 12:29:30
**什麼是錯誤**? – gdoron 2012-03-16 12:31:37