通过javascript警告文本框

时间:2011-11-02 04:39:39

标签: php javascript html

非常简单的问题!我想知道如何使用javascript来提醒我的文本框中的内容......不确定我现在正在做什么是不行的。继承人的功能

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

现在这里是文本框

<input type = 'text' name = 'txt_comment'>
<button onClick = alertNow(txt_comment.value) value = "Submit Comment"></button> 
确定这是可行的,我之前做过,但我忘记了一些语法问题。有任何想法吗?谢谢!

5 个答案:

答案 0 :(得分: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> 

答案 1 :(得分:0)

<input type="text" id="testTextarea">
<input type="submit" value="Test" onClick="alert(document.getElementById('testTextarea').value);" />

答案 2 :(得分: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>

答案 3 :(得分:0)

这是完成任务的代码......

// js code ...

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>  

答案 4 :(得分: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>