获取使用Java脚本在输入框中输入的文本

时间:2019-03-17 20:40:39

标签: javascript html

我是javascript新手,我正在尝试创建一个网页,我们可以在其中输入 输入框中的名称,并使用javascript的alert方法显示一个警告框,上面显示您好以及我们在输入框中输入的名称     我的代码是

    //html
<input id="test" type="text" name="nm" placeholder="Enter name">
<button onclick="fun(i dont know what to pass here in order to get the text entered in that text box)" type="button" name="button">Click here</button>

   //javascript
//i tried like this...first

function fun(x) {
  alert("HELLO" + x);
}

//i tried this...then
var x = document.getElementsById("test").value; [->here i also dono what to put.]

function fun(x) {
  alert("HELLO" + x);
}

1 个答案:

答案 0 :(得分:0)

应为getElementById,而不是getElementsById。希望对您有所帮助,并祝您学习愉快:)

您的代码:

function fun() {
  var textInTest = document.getElementById("test").value;
  alert("Value entered" + textInTest);
}
<input id="test" type="text" name="nm" placeholder="Enter name">
<button onclick="fun()" type="button" name="button">Click here</button>