簡體   English   中英

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM