문제

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?

도움이 되었습니까?

해결책

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

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top