2015-12-03 45 views
0

所以我試圖讓每次用戶提交輸入時清除文本輸入按回車。我試過$("#textbox").val(''),但這不起作用,我不知道我做錯了什麼。我也試過.html('').text('')。我不知道爲什麼它不起作用。我不能得到.val('')來清除用戶輸入,我不知道爲什麼它不起作用

這是我的編碼。

// JavaScript Document 
 

 
$(document).ready(function() { 
 
    $('a').click(function() { 
 
    $('#start').css('background-color', '#09C'); 
 
    }); 
 
}); 
 

 
//Item variable// 
 

 
$(document).ready(function() { 
 
    $("form").submit(function() { 
 
    var input = $("#command").val(); 
 
    var check = false; 
 

 
    if (input == "help") { 
 
     $("#Help").clone().insertBefore("#placeholder").fadeIn(1000); 
 
     check(); 
 
    } 
 

 

 
    if (input == "Lombardy East") { 
 
     $("<p class='text'> Great Choice! Close enough to Alexandra to have access to their commuters but far enough not to step on anyone's toes! Now it's time to choose your route. Shall we?</p>").hide().insertBefore("#placeholder").fadeIn(1000); 
 
     $("<p id='green' class='text'>Let's do this?</p>").hide().insertBefore("#placeholder").fadeIn(1000); 
 
     check(); 
 
     check(); 
 
    } 
 

 
    if (input == "Lyndhurst") { 
 
     $("<p class='text'> It's good enough choice. As long as you don't clash with other drivers on the main road, you should be fine. Now it's time to choose your route. Shall we?</p>").hide().insertBefore("#placeholder").fadeIn(1000); 
 
     $("<p id='green' class='text'>Okay?</p>").hide().insertBefore("#placeholder").fadeIn(1000); 
 
     check(); 
 
     check(); 
 
    } 
 

 
    if (input == "Orange Grove") { 
 
     $("<p class='text'>Hmmm ... That's a bit close to Louis Botha main road. You might have a few aggressive run ins. Good Luck with that.Now it's time to choose your route. Shall we?</p>").hide().insertBefore("#placeholder").fadeIn(1000); 
 
     $("<p id='green' class='text'>Sure?</p>").hide().insertBefore("#placeholder").fadeIn(1000); 
 
     check(); 
 
     check(); 
 
    } 
 

 
    if (input == "No") { 
 
     $("<p class='text'> Voetsak! Don't be a wuss!</p>").hide().insertBefore("#placeholder").fadeIn(1000); 
 
     check(); 
 
    } 
 

 
    if (input == "Yes") { 
 
     var url = "thirdpage.html"; 
 
     $(location).attr('href', url); 
 
     check();; 
 
    } 
 

 
    if (input == "Let's do this") { 
 
     var url = "frthpage.html"; 
 
     $(location).attr('href', url); 
 
     check(); 
 
    } 
 

 
    if (input == "Okay") { 
 
     var url = "ffthpage.html"; 
 
     $(location).attr('href', url); 
 
     check(); 
 
    } 
 

 
    if (input == "Sure") { 
 
     var url = "sxthpage.html"; 
 
     $(location).attr('href', url); 
 
     check(); 
 
    } else if (input != "Lombardy East") { 
 
     $("<p class='text'> I do not understand your answer</p>").hide().insertBefore("#placeholder").fadeIn(1000); 
 
     check(); 
 
    } 
 

 
    $('#command').val(""); 
 

 
    }); 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> 
 
<div id="placeholder"></div> 
 
<form onsubmit="return false" autocomplete="off"> 
 
    <input type="text" size="50" autofocus="autofocus" id="command" /> 
 
</form>

+0

「我試過$(」 #文本框 「)VAL( '')。」 - >不是輸入的識別碼你的html代碼。 –

+0

由於'check'是'FALSE',代碼將拋出一個錯誤,並在第一個'檢查();'遇到,從來沒有得到儘可能'$(「#指令」)VAL(「」);' 。 –

回答

0

的問題是不與您試圖清除輸入的方式。

$('#command').val(""); 

這將工作,但你必須在頁面上的JavaScript錯誤。

問題是你的檢查變量,你初始化一個變量檢查​​爲一個基本類型。

var check = false; 

這是一個布爾值而不是函數。所以當你試圖調用變量檢查,因爲它不是一個函數,你會得到一個錯誤。

我不確定你想用檢查函數調用完成什麼,但是你有兩個選擇。

1.)刪除所有的檢查();由於check是原始類型(布爾型),因此它不能作爲函數調用。

2)你可以做檢查功能...

var check = function() { 
    // Do Logic here 
} 
+0

哦,我明白了。謝謝。我刪除了所有的檢查();它的工作原理。我真的不知道我在用jQuery做什麼,我只是從它開始。 – Mooimiz

相關問題