2015-06-14 59 views
0

我有一個代碼,我希望在用戶鍵入textfield中的某些內容後按下回車鍵,屏幕上會顯示他輸入的內容。但我無法做到這一點,我想在這裏得到一些幫助。顯示用戶在主體中鍵入的內容

<!DOCTYPE html> 
<html> 
<head> 
<title>Tasks for the day</title> 
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script> 

<script> 

alert("When you have finished your task you only have to click on it."); 

$(document).ready(function(){ 
$("p").click(function(){ 
    $(this).hide(); 
}); 
}); 

$(document).keypress(function(e) { 
if(e.which == 13) { 

} 
}); 

function showMsg(){ 
var userInput = document.getElementById('userInput').value; 
    document.getElementById('userMsg').innerHTML = userInput; 
} 

</script> 
</head> 
<body> 

<h1>Tasks to do</h1> 

<p>Type what you need to do:</p> 

<input type="input" id="userInput" onkeyup=showMsg() value="" /> 
<p id="userMsg"></p> 

</body> 
</html> 
+0

剛剛試過代碼,它的工作。這裏有什麼確切的問題?當他們按下輸入鍵清除屏幕上除了值之外的所有內容時,是否需要? – AmmarCSE

+0

當用戶在框中鍵入內容時,他將按回車。當他按下輸入時,我希望屏幕上顯示的是輸入的內容。 –

+1

是什麼? https://jsfiddle.net/v58ugsg5/ – AmmarCSE

回答

0

它只是增加了一個值到屏幕上,把更多的不是一個,我需要 創建一個數組

  1. 你有工作的主要組成部分。即,您正在更新屏幕。有它只是在enter更新,簡單地把代碼中keypress處理
  2. 要將值添加到屏幕(在不止一個enter的情況下),與價值

串聯當前innerHTML

$(document).ready(function() { 
 
    $("p").click(function() { 
 
    $(this).hide(); 
 
    }); 
 
}); 
 

 
$('#userInput').keypress(function(e) { 
 
    if (e.which == 13) { 
 
    var userInput = document.getElementById('userInput').value; 
 
    var innerHTML = document.getElementById('userMsg').innerHTML; 
 
    innerHTML = innerHTML || ''; 
 
    document.getElementById('userMsg').innerHTML += innerHTML + userInput; 
 
    } 
 
});
<title>Tasks for the day</title> 
 
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script> 
 

 
<h1>Tasks to do</h1> 
 

 
<p>Type what you need to do:</p> 
 

 
<input type="input" id="userInput" onkeyup=showMsg() value="" /> 
 
<p id="userMsg"></p>

相關問題