通过javascript提示文本框


alerting textbox through javascript

很简单的问题!我想知道如何使用javascript来提醒我的文本框中的东西…我不确定我现在做的是不是有效。下面是函数

function alertNow(comment){
alert(comment);
}
这里是文本框
<input type = 'text' name = 'txt_comment'>
<button onClick = alertNow(txt_comment.value) value = "Submit Comment"></button> 

肯定这是可行的,我以前做过,但我只是忘记了一些语法问题。什么好主意吗?谢谢!

您缺少报价。同时使用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> 
<input type="text" id="testTextarea">
<input type="submit" value="Test" onClick="alert(document.getElementById('testTextarea').value);" />
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>

下面是完成任务的代码…

//js代码…

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

//HTML代码....

<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>  

使用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>