问题与jQuery数据()处理字符串为数字 [英] Issue with jQuery data() treating string as number
问题描述
< div data-id =211285677671858177 >
和JavaScript:
var send_data = {
id:''+ $(this).data('id')
}
$ .post('/ send.php' ,send_data);
问题在于jQuery data
函数似乎检索该值作为一个浮点而不是一个字符串。所以把它附加到一个空白的字符串是没有帮助的,因为它已经太晚了 - 已经被四舍五入了(在这个例子中是 211285677671858180
)。我可以做些什么来解决这个问题?
这实际上并不是long int的情况,重新获得是最接近的可用表示形式作为浮点数。
无论如何,你想要一个字符串的值。引用针对 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. And the JavaScript: The issue is that the jQuery 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 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屋! .data
的jQuery文档(重点我的每一个尝试都是将字符串转换为一个JavaScript值
(这包括布尔值,数字,对象等等)。
$ b
它保留为一个字符串。 要将值的属性作为字符串
检索,而不尝试转换它,请使用attr()方法。
<div data-id="211285677671858177">
var send_data = {
id: '' + $(this).data('id')
}
$.post('/send.php', send_data);
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?.data
(emphasis mine):