简体   繁体   English

通过javascript警告文本框

[英]alerting textbox through javascript

very simple question! 很简单的问题! i was wondering how to use javascript to alert something in my textbox...not sure what i'm doing now is not working. 我想知道如何使用javascript来提醒我的文本框中的内容......不确定我现在正在做什么是不行的。 heres the function 继承人的功能

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

now here is the textbox 现在这里是文本框

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

sure this is doable, i did it before but i just forgot some syntax prob. 确定这是可行的,我之前做过,但我忘了一些语法概率。 any ideas? 有任何想法吗? thanks! 谢谢!

You are missing quotations. 你缺少报价。 Also use the ID instead of name. 也使用ID而不是名称。

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

Also better to use, document.getElementById like below 还有更好的使用方法,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>

here is the code to complete your task... 这是完成任务的代码......

// js code ... // js代码......

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

// html code .... // 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>  

Use the ID instead of name. 使用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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM