问题与jQuery数据()处理字符串为数字 [英] Issue with jQuery data() treating string as number

查看:135
本文介绍了问题与jQuery数据()处理字符串为数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个MySQL的BIGINT,我在HTML5数据存储。然后我尝试访问该值,并通过AJAX调用传递。

 < div data-id =211285677671858177 > 

和JavaScript:

  var send_data = {
id:''+ $(this).data('id')
}
$ .post('/ send.php' ,send_data);

问题在于jQuery data 函数似乎检索该值作为一个浮点而不是一个字符串。所以把它附加到一个空白的字符串是没有帮助的,因为它已经太晚了 - 已经被四舍五入了(在这个例子中是 211285677671858180 )。我可以做些什么来解决这个问题?

解决方案

这实际上并不是long int的情况,重新获得是最接近的可用表示形式作为浮点数。

无论如何,你想要一个字符串的值。引用针对 .data 的jQuery文档(重点我的每一个尝试都是将字符串转换为一个JavaScript值
(这包括布尔值,数字,对象等等)。
$ b 数组和空值)否则
它保留为一个字符串。 要将值的属性作为字符串
检索,而不尝试转换它,请使用attr()方法。


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.

<div data-id="211285677671858177">

And the 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?

解决方案

This isn't a case of "long int" really, the number you're getting is the closest available representation as a floating-point number.

Anyway, you want the value as a string. Quote the jQuery docs for .data (emphasis mine):

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.

这篇关于问题与jQuery数据()处理字符串为数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆