javascript javascript没有获取输入框的值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5732188/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 18:09:21  来源:igfitidea点击:

javascript not getting value of input box

javascripttextboxgetelementbyid

提问by AltheFuzz

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!

我似乎无法将输入框的值传递给我的 javascript 中的任何其他内容。它不会产生任何错误——我记得在某处读到过,如果文档没有完成加载,你可能会遇到问题——但我很确定它有!

The code in question is as follows in the javascript:

有问题的代码在javascript中如下:

var address = getElementById(addyInput).value;
document.getElementById('add').innerHTML = address;

And in the HTML

在 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!!

我现在有它的工作!非常感谢所有的帮助!!

回答by Mark Biek

Your form needs to look like this (add an id attribute):

您的表单需要如下所示(添加 id 属性):

<form>
<input id="addyInput" name="addyInput" placeholder="Don't forget postcode!">
</form>

And the first line of Javascript needs to look like this (since getElementByIdis expecting an ID rather than a name).

Javascript 的第一行需要看起来像这样(因为需要的getElementById是 ID 而不是名称)。

var address = getElementById('addyInput').value;

Additionally, getElementByIdexpects the id argument to be a string (hence the quotes). If you pass it addyInputwithout quotes, it'll try to interpret addyInputas a variable which has a value of undefined and you won't get back the DOM element you want.

此外,getElementById期望 id 参数是一个字符串(因此是引号)。如果您addyInput不带引号传递它,它会尝试将其解释addyInput为具有 undefined 值的变量,并且您将无法取回您想要的 DOM 元素。



Or, if you were using jQuery, you could leave the form markup as-is and change the Javascript to this:

或者,如果您使用的是jQuery,您可以保留表单标记原样并将 Javascript 更改为:

var address = $('input[name=addyInput]').val();

回答by Paul Perigny

Make sure to specify and idon the input. You only have a name.

确保id在输入上指定和。你只有一个name.

回答by mattholl

You need to add the id "addyInput" to your form input rather than just the name.

您需要将 id "addyInput" 添加到表单输入中,而不仅仅是名称。

回答by KebdnK

getElementById expects a string.

getElementById 需要一个字符串。

var address = getElementById('addyInput').value;

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.

当然,您应该像其他人所说的那样为输入元素定义一个 id。

回答by Sherif Riad

what you are getting is an array, you need to fetch your array into some readable data. Try something like:

您得到的是一个数组,您需要将数组提取到一些可读数据中。尝试类似:

  • $value = array_shift( $yourarray );
  • $value = array_shift( $yourarray );

or if it's a multi value array you can just loop it to fetch out the values.

或者如果它是一个多值数组,您可以循环它以获取值。