javascript jQuery data() 将字符串视为数字的问题
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10958047/
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
Issue with jQuery data() treating string as number
提问by andrewtweber
I have a MySQL BIGINT that I am storing in HTML5 data. Then I'm trying to access that value and pass it through an AJAX call.
我有一个 MySQL BIGINT,我将它存储在 HTML5 数据中。然后我试图访问该值并通过 AJAX 调用传递它。
<div data-id="211285677671858177">
And the JavaScript:
和 JavaScript:
var send_data = {
id: '' + $(this).data('id')
}
$.post('/send.php', send_data);
The issue is that the jQuery data
function seems to retrieve that value as a floating point and not a string. So appending it to a blank string isn't helping because it's already too late - it's already been rounded (in this case to 211285677671858180
). What can I do to fix this?
问题是 jQuerydata
函数似乎将该值作为浮点而不是字符串检索。所以将它附加到一个空白字符串并没有帮助,因为它已经太晚了 - 它已经四舍五入(在这种情况下为211285677671858180
)。我能做些什么来解决这个问题?
回答by John Flatness
This isn't a case of "long int" really, the number you're getting is the closest available representation as a floating-point number.
这实际上不是“long int”的情况,您得到的数字是作为浮点数的最接近的可用表示。
Anyway, you want the value as a string. Quote the jQuery docs for .data
(emphasis mine):
无论如何,您希望该值作为字符串。引用jQuery 文档.data
(强调我的):
Every attempt is made to convert the string to a JavaScript value (this includes booleans, numbers, objects, arrays, and null) otherwise 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.
每次尝试将字符串转换为 JavaScript 值(这包括布尔值、数字、对象、数组和空值),否则将其保留为字符串。要将值的属性作为字符串检索而不尝试进行任何转换,请使用 attr() 方法。
回答by Pointy
There's no such thing as a "long integer" in JavaScript. All numbers are 64-bit floating point. Your number there cannot be represented exactly in IEEE-794 floating point representation.
JavaScript 中没有“长整数”这样的东西。所有数字都是 64 位浮点数。您的数字无法在 IEEE-794 浮点表示中准确表示。
The only way to "fix" this is to make the number into a string in a domain that can cope with big integer values.
“修复”这个问题的唯一方法是将数字转换为域中可以处理大整数值的字符串。
回答by TK123
try typecasting it to string:
尝试将其类型转换为字符串:
id: '' + $(this).data('id').toString();
回答by moribvndvs
Try surrounding the data attribute value in single quotes, if you want data
to treat it like a string. Problem there, I guess, is you'll have the quotes to strip.
如果要将数据属性值data
视为字符串,请尝试将其括在单引号中。问题是,我想,您将要删除引号。
回答by Abhishek Shrivastava
I had a similar issue where the data id (alphanumeric) was getting converted through usage of .data() as a result the last few characters were getting converted to all zeroes. It took some research to determine that this is a bug in jQuery implementation of .data().
我有一个类似的问题,数据 ID(字母数字)通过使用 .data() 被转换,结果最后几个字符被转换为全零。经过一些研究,确定这是 .data() 的 jQuery 实现中的一个错误。
Reference: http://bugs.jquery.com/ticket/7579
参考:http: //bugs.jquery.com/ticket/7579
For me, to get the data id directly as a work around, I used .attr("data-id") directly and this gave me the correct id.
对我来说,为了直接获取数据 id 作为解决方法,我直接使用了 .attr("data-id") 这给了我正确的 id。