2016-07-31 19 views
0

我有一些麻煩讓我的表單爲空,一旦任何文本已附加它不明確。當它不是一個單獨的函數時,它曾經很好地工作,我希望代碼能夠和enter一起工作,這似乎是最簡單的方法。我試過其他的方法,如.clear和.reset,但他們似乎重新加載我的頁面,清除任何創建的div。我嘗試過移動代碼以及創建一個完全獨立的函數,兩者都沒有效果。我也沒有收到我的控制檯中的任何錯誤消息。有沒有人有任何建議,爲什麼這可能會發生?不能清空表格

var template = function(text) { 
    return '<div class="event"><p>'+ text + ' -</p></div>' 
}; 
var process = function(){ 
    var text = $('#action').val(); 
    var html = template(text); 
    $(html).appendTo('.controller'); 
    $('#action').val() === ""; 
    $('.controller').scrollTop($('.controller')[0].scrollHeight); 
    return false; 

}; 
var main = function(){ 
    $('.submit').click(process); 
    $(13).keyup(process); 
}; 

$(document).ready(main); 

HTML

<!DOCTYPE html> 
<html> 
<head> 
    <title>Lord of the Deathsticks</title> 
    <link href="css/text-game.css" rel="stylesheet" type="text/css"> 
</head> 

<body> 
    <div class="info"> 
     <h1 class="title">Lord of the Deathsticks</h1> 
    </div> 
    <div class="controller"> 
     <div class="event"> 
      <p>Hello!</p> 
     </div> 
     <div class="event"> 
      <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p> 
     </div> 
    </div> 
    <form class="form"> 
     <div class="form-container"> 
      <input type="text" class="input" id="action" placeholder="What will you do?"> 
      <button class="submit" type="btn">Ok</button> 
     </div> 
    </form> 


<script src="js/controllers/controller.js"></script> 
<script src="js/model/characters.js"></script> 
<script src="js/JQuery/jquery-3.1.0.js"></script> 
<script src="js/JQuery/text-game-JQuery.js"></script> 
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script> 

</body> 
    </html> 
+0

ID爲'action'的元素在哪裏? Plz顯示你的HTML。無論如何,要清除動作值,請嘗試使用$('#action')。val(「」);'而不是'$('#action')。val()===「」;' –

+0

顯示HTML代碼 – jonju

+0

好的,只需加上 – l34lo1

回答

1

它看起來只是.val()濫用。

要清除一個值,使用

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

正因爲如此:

$('#action').val() === ""; 

是一個比較...,並將所得真/假簡直就不​​是在一個變量抓獲。
jQuery沒有腳本錯誤...這是程序員的邏輯錯誤。

請參閱codePen here

+0

非常感謝!它工作完美 – l34lo1