最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - JSON.stringify() <input> values as numbers? - Stack Overflow

programmeradmin1浏览0评论

I am using JSON.stringify() on html <input>s to send through a websocket like so:

JSON.stringify({
    numberValue: $('#numberValue').val()
})

but it encodes $('#numberValue').val() as a String.

How can it be encoded as a Number?

I am using JSON.stringify() on html <input>s to send through a websocket like so:

JSON.stringify({
    numberValue: $('#numberValue').val()
})

but it encodes $('#numberValue').val() as a String.

How can it be encoded as a Number?

Share Improve this question asked Aug 18, 2013 at 19:28 user1382306user1382306
Add a comment  | 

2 Answers 2

Reset to default 11

Convert it to an integer first.

JSON.stringify({
    numberValue: parseInt($('#numberValue').val(), 10);
})

You can parse the string:

JSON.stringify({
    numberValue: parseInt($('#numberValue').val(), 10);
})

The parseInt() function parses a string and returns an integer.

More info: http://www.w3schools.com/jsref/jsref_parseint.asp

发布评论

评论列表(0)

  1. 暂无评论