[英]How can I get the value of an input element instead of `null`?
When the user inputs any number, the number needs to be joined with the assigned text (in this case a URL).当用户输入任何数字时,该数字需要与分配的文本(在本例中为 URL)相结合。 However it results in this:
http://someurl.com/?value=null
.然而,结果是:
http://someurl.com/?value=null
。 Instead of null
I expect the number the user inputted.我希望用户输入的数字不是
null
。
<head>
<script type="text/javascript">
var juarel = "http://someurl.com/?value=";
var velju = document.getElementById('somenumber');
var rezon = juarel.concat(velju);
function print() {
alert(rezon);
}
</script>
</head>
<body>
<form>
<input type="number" id="somenumber" value="">
<button type="button" id="submit" name="button" onclick="print()">SUBMIT</button>
</form>
</body>
Get the correct value from the HTML element calling .value
.从调用
.value
的 HTML 元素中获取正确的值。 Then put the code to retrieve the value inside the function, otherwise you are reading the value at the beginning, and the element is empty.然后把取值的代码放在函数里面,否则你是在开头读取值,元素为空。 You have to read the value when you press the button:
当您按下按钮时,您必须读取该值:
<body> <form> <input type="number" id="somenumber" value=""> <button type="button" id="submit" name="button" onclick="print()">SUBMIT</button> </form> <script type="text/javascript"> function print () { var juarel = "http://someurl.com/?value="; var velju = document.getElementById('somenumber').value; var rezon = juarel.concat(velju); alert(rezon); } </script> </body>
1st use .value
as var velju = document.getElementById('somenumber').value
第一次使用
.value
作为var velju = document.getElementById('somenumber').value
2nd wrap those into the function, so when you click, you get the current value, otherwise, it is always undefined since it is called on init. 2nd 将它们包装到函数中,所以当你点击时,你会得到当前值,否则,它总是未定义的,因为它在 init 上被调用。
function print() {
var juarel = "http://someurl.com/?value=";
var velju = document.getElementById('somenumber').value;
var rezon = juarel.concat(velju);
alert(rezon);
}
<script type="text/javascript"> function print() { var juarel = "http://someurl.com/?value="; var velju = document.getElementById('somenumber').value; var rezon = juarel.concat(velju); alert(rezon); } </script> <body> <form> <input type="number" id="somenumber" value=""> <button type="button" id="submit" name="button" onclick="print()">SUBMIT</button> </form>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.