2011-11-02 63 views
0

非常簡單的問題!我想知道如何使用JavaScript來提醒我的文本框中的東西...不知道我現在正在做什麼不工作。繼承人的功能通過javascript提醒文本框

function alertNow(comment){ 
alert(comment); 
} 

現在這裏是文本框

<input type = 'text' name = 'txt_comment'> 
<button onClick = alertNow(txt_comment.value) value = "Submit Comment"></button> 

相信這是可行的,我以前做過,但我只是忘了一些語法的概率。有任何想法嗎?謝謝!

回答

2

您缺少報價。也使用ID而不是名稱。

<input type = 'text' name = 'txt_comment' id='txt_comment'> 
<button onClick = 'alertNow(txt_comment.value)' value = "Submit Comment"></button> 

而且更好地使用,喜歡的document.getElementById下面

<button onClick = 'alertNow(document.getElementById("txt_comment").value)' value = "Submit Comment"></button> 
0
<input type="text" id="testTextarea"> 
<input type="submit" value="Test" onClick="alert(document.getElementById('testTextarea').value);" /> 
0
function alertNow() { 
var a = document.getElementById("txt_comment").value; 
alert (a); 
} 

<input type = 'text' name = 'txt_comment' id="txt_comment"> 
<button onClick = "alertNow()" value= "Submit Comment"></button> 
0

這裏是代碼來完成你的任務......

// js代碼...

function alertNow(){ 
    alert(document.getElementById('txt_comment').value); 
    return false; 
} 

// html code ....

<form name="form_1" id="form_1" method="post"> 
    <input type = 'text' id='txt_comment' name = 'txt_comment'> 
    <input type="button" onClick = "return alertNow();" value = "Submit Comment"/> 
</form> 
0

使用ID而不是名稱。

function alertText() { alert(document.getElementById('txt_comment').value); }

<input type = 'text' name = 'txt_comment' id="txt_comment"> 
<button onClick = "alertText()" value= "Submit Comment"></button>