javascript JSON.stringify() <input> 值作为数字?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18302890/
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
JSON.stringify() <input> values as numbers?
提问by Matt Bryant
I am using JSON.stringify()on html <input>s to send through a websocket like so:
我JSON.stringify()在 html 上使用<input>像这样通过 websocket 发送:
JSON.stringify({
numberValue: $('#numberValue').val()
})
but it encodes $('#numberValue').val()as a String.
但它编码$('#numberValue').val()为String.
How can it be encoded as a Number?
如何将其编码为Number?
回答by Matt Bryant
Convert it to an integer first.
首先将其转换为整数。
JSON.stringify({
numberValue: parseInt($('#numberValue').val(), 10);
})
回答by amatellanes
You can parse the string:
您可以解析字符串:
JSON.stringify({
numberValue: parseInt($('#numberValue').val(), 10);
})
The parseInt() function parses a string and returns an integer.
parseInt() 函数解析一个字符串并返回一个整数。
More info: http://www.w3schools.com/jsref/jsref_parseint.asp

