Javascript document.getElementById().value 不设置值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13553497/
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-08-24 12:21:02  来源:igfitidea点击:

document.getElementById().value doesn't set the value

javascripthtml

提问by Deus Deceit

Browser is chromium (under ubuntu)

浏览器是chrome(在ubuntu下)

This is a chunk of code: (of course) The alert messages shows the right value. but the points element doesn't get the right value, it actually gets empty. Can anybody tell me why?

这是一段代码:(当然)警报消息显示了正确的值。但是 points 元素没有得到正确的值,它实际上变空了。谁能告诉我为什么?

Here's how points element is defined.

以下是点元素的定义方式。

<input type='number' id='points' value='0'/> 

and here is how javascript code is supposed to populate it.

这是 javascript 代码应该如何填充它。

alert(request.responseText);

document.getElementById("points").value=request.responseText;

回答by slashingweapon

Your response is almost certainly a string. You need to make sure it gets converted to a number:

您的响应几乎肯定是一个字符串。您需要确保将其转换为数字:

document.getElementById("points").value= new Number(request.responseText);

You might take a closer look at your responseText. It sound like you are getting a string that contains quotes. If you are getting JSON data via AJAX, you might have more consistent results running it through JSON.parse().

您可能会仔细查看您的 responseText。听起来您正在获取一个包含引号的字符串。如果您通过 AJAX 获取 JSON 数据,则通过JSON.parse().

document.getElementById("points").value= new Number(JSON.parse(request.responseText));

回答by Quentin

According to my tests with Chrome:

根据我对 Chrome 的测试:

If you set a numberinput to a Number, then it works fine.

如果您将number输入设置为数字,则它可以正常工作。

If you set a numberinput to a String that contains nothing but a number, then it works fine.

如果您将number输入设置为只包含数字的字符串,则它可以正常工作。

If you set a numberinput to a String that contains a number and some whitespace, then it blanks the input.

如果您将number输入设置为包含数字和一些空格的字符串,则会将输入设为空白。

You probably have a space or a new line after the data in the server response that you actually care about.

在您真正关心的服务器响应中的数据之后,您可能有一个空格或一个新行。

Use document.getElementById("points").value = parseInt(request.responseText, 10);instead.

使用document.getElementById("points").value = parseInt(request.responseText, 10);来代替。

回答by jAndy

The only case I could imagine is, that you run this on a webkitbrowser like Chromeor Safariand your return value in responseText, contains a string value.

我能想象的唯一情况是,您在ChromeSafariwebkit浏览器上运行它,并且您在 中的返回值包含一个字符串 valueresponseText

In that constelation, the value cannot be displayed (it would get blank)

在那个星座中,该值无法显示(它会变成空白)

Example: http://jsfiddle.net/BmhNL/2/

示例:http: //jsfiddle.net/BmhNL/2/



My point here is, that I expect a wrong/double encoded string value. Webkit browsers are more strict on the type= number. If there is "only" a white-space issue, you can try to implicitly call the Number()constructor, like

我的观点是,我期望错误/双重编码的字符串值。Webkit 浏览器对type=更严格number。如果“只有”一个空白问题,您可以尝试隐式调用Number()构造函数,例如

document.getElementById("points").value = +request.responseText;

回答by Karan

The problem is clearly not with the javascript. Here's a quick snippet to show you the working of the code.

问题显然不在于javascript。这是一个快速片段,向您展示代码的工作方式。

document.getElementById('points').value = 100;

http://jsfiddle.net/eqTm2/1/

http://jsfiddle.net/eqTm2/1/

As you can see, the javascript perfectly assigns the new value to the input element. It would be helpful if you could elaborate more on the issue.

如您所见,javascript 完美地将新值分配给输入元素。如果您能详细说明这个问题,将会很有帮助。

回答by Lova Chittumuri

Refer below Code Snip

参考下面的代码片段

In HTML Page

在 HTML 页面中

<input id="test" type="number" value="0"/>

In Java Script

在 Java 脚本中

document.getElementById("test").value=request.responseText;// in case of String 
document.getElementById("test").value=new Number(responseText);// in case of Number

回答by krohit

Try using ,

尝试使用,

$('#points').val(request.responseText);