2011-11-02 62 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>