javascript无法获取输入框的值
我似乎无法将输入框的值传递给 JavaScript 中的其他内容。 它不会产生任何错误 - 我记得在某处读过,如果文档尚未完成加载,您可能会遇到问题 - 但我很确定它已经完成!
有问题的代码在 javascript 中如下所示:
var address = getElementById(addyInput).value;
document.getElementById('add').innerHTML = address;
在 HTML 中,
<form>
<input name="addyInput" placeholder="Don't forget postcode!">
</form>
<button id="start" onclick="initialize()">Start!</button>
<p>Address Test
<div id="add"></div>
</p>
我知道按钮本身正在工作,因为它可以正常触发我的其余代码,而不会出现违规代码 - 然而,当我取消注释顶部的那个小块时,它只是什么也不做。 (没有错误等)
对此的任何帮助都会很热!谢谢:)
更新:
我现在可以使用了!非常感谢您的所有帮助!
I seem to be having trouble with passing the value of an input box to anything else in my javascript.
It's not producing any errors - and I remember reading somewhere that you can have issues if the document hasn't finished loading - but I'm pretty sure it has!
The code in question is as follows in the javascript:
var address = getElementById(addyInput).value;
document.getElementById('add').innerHTML = address;
And in the HTML
<form>
<input name="addyInput" placeholder="Don't forget postcode!">
</form>
<button id="start" onclick="initialize()">Start!</button>
<p>Address Test
<div id="add"></div>
</p>
I know that the button itself is working as it fires the rest of my code fine without the offending code - however the moment I uncomment that little block at the top, it just does nothing. (no errors etc)
Any help on that one would be hot! Thanks :)
Update:
I now have it working! Thanks muchly for all the help!!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您的表单需要如下所示(添加 id 属性):
并且 Javascript 的第一行需要如下所示(因为
getElementById
需要 ID 而不是名称)。此外,
getElementById
期望 id 参数是一个字符串 (因此引用)。如果你传递给它的addyInput
不带引号,它会尝试将addyInput
解释为一个值为 undefined 的变量,并且你不会得到你想要的 DOM 元素。或者,如果您使用 jQuery,您可以按原样保留表单标记并将 Javascript 更改为:
Your form needs to look like this (add an id attribute):
And the first line of Javascript needs to look like this (since
getElementById
is expecting an ID rather than a name).Additionally,
getElementById
expects the id argument to be a string (hence the quotes). If you pass itaddyInput
without quotes, it'll try to interpretaddyInput
as a variable which has a value of undefined and you won't get back the DOM element you want.Or, if you were using jQuery, you could leave the form markup as-is and change the Javascript to this:
确保在输入中指定
id
。您只有一个名字
。Make sure to specify and
id
on the input. You only have aname
.您需要将 id“addyInput”添加到表单输入中,而不仅仅是名称。
You need to add the id "addyInput" to your form input rather than just the name.
getElementById 需要一个字符串。
var address = getElementById('addyInput').value;
如果你直接把这个放到head的脚本部分,那么你就会遇到问题,因为页面没有完全加载但是代码却被执行了已经。
当然,正如其他人已经说过的那样,您应该为输入元素定义一个 id。
getElementById expects a string.
var address = getElementById('addyInput').value;
If you put this directly into a script section in the head, then you will have a problem because the page is not loaded completely but the code is executed already.
And of course you should define an id for the input element as the others already said.
你得到的是一个数组,你需要将数组提取到一些可读的数据中。尝试类似:
$value = array_shift( $yourarray );
或者如果它是一个多值数组,您可以循环它来取出值。
what you are getting is an array, you need to fetch your array into some readable data. Try something like:
$value = array_shift( $yourarray );
or if it's a multi value array you can just loop it to fetch out the values.