Вопрос

I am using a basic identifier which uses a unix timestamp with an appended alphanumeric section appended to it.

Everything has been working fine until recently - a couple of identifiers fail to update in the database.

I have noticed that the failing id's have all numbers and the letter E.

EG 1386953039E87 which is being transcribed as 1.386953039e+96

I am far from being a mathematician but feel that the original value is being treated as a number. I have tried using the toString() function but this has not worked for me.

Это было полезно?

Решение

Calling toString is too late because outputting 1386953039E87 has already created it as a number (or JavaScript's best guess at a number).

Try modifying your server-side code to output it surround in quotes instead, so that it gets created as a string instead of a number.

This could also be a side-effect of using jQuery's data() function. They mention in their api docs that it doesn't alter their value but it seems like your situation is proving otherwise:

Every attempt is made to convert the string to a JavaScript value (this includes booleans, numbers, objects, arrays, and null). A value is only converted to a number if doing so doesn't change the value's representation. For example, "1E02" and "100.000" are equivalent as numbers (numeric value 100) but converting them would alter their representation so they are left as strings. The string value "100" is converted to the number 100.

When the data attribute is an object (starts with '{') or array (starts with '[') then jQuery.parseJSON is used to parse the string; it must follow valid JSON syntax including quoted property names. If the value isn't parseable as a JavaScript value, it is left as a string.

To retrieve the value's attribute as a string without any attempt to convert it, use the attr() method.

So it seems you need to use the attr() instead of data() for this

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top