We have this 'strange' situation where some product codes, for example 11E6, which are stored in data attributes (ex data-prodcode) are getting converted to 11000000, when retrieved inside jquery click function. Something like this:
我们有这种“奇怪”的情况,当在jquery点击功能中检索时,存储在数据属性(ex data-prodcode)中的某些产品代码(例如11E6)将转换为11000000。像这样的东西:
<a data-prodcode="11E6">click</a>
var code = $(this).data('prodcode');
console.log(code); --> 11000000
Any advice on how to avoid this behavior or what may cause it?
关于如何避免这种行为或可能导致这种行为的任何建议?
1 个解决方案
#1
7
From the documentation :
从文档:
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值(这包括布尔值,数字,对象,数组和null),否则它将保留为字符串。要将值的属性检索为字符串而不尝试转换它,请使用attr()方法。
You may use attr
in order to avoid automatic parsing :
您可以使用attr以避免自动解析:
var code = $(this).attr('data-prodcode');
To be more precise : this shouldn't happen. And in fact it doesn't happen in last versions. Here's the code of current's jQuery (the most interesting part is the comment) :
更确切地说:这不应该发生。事实上,它不会发生在最新版本中。这是当前jQuery的代码(最有趣的部分是注释):
if ( typeof data === "string" ) {
try {
data = data === "true" ? true :
data === "false" ? false :
data === "null" ? null :
// Only convert to a number if it doesn't change the string
+data + "" === data ? +data :
rbrace.test( data ) ? jQuery.parseJSON( data ) :
data;
} catch( e ) {}
And it works in jQuery 1.8 and 1.9 : it doesn't convert the string to a number if a back conversion doesn't produce the same string. But it didn't work in jQuery 1.7.
它适用于jQuery 1.8和1.9:如果后向转换不产生相同的字符串,它不会将字符串转换为数字。但它在jQuery 1.7中不起作用。
#1
7
From the documentation :
从文档:
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值(这包括布尔值,数字,对象,数组和null),否则它将保留为字符串。要将值的属性检索为字符串而不尝试转换它,请使用attr()方法。
You may use attr
in order to avoid automatic parsing :
您可以使用attr以避免自动解析:
var code = $(this).attr('data-prodcode');
To be more precise : this shouldn't happen. And in fact it doesn't happen in last versions. Here's the code of current's jQuery (the most interesting part is the comment) :
更确切地说:这不应该发生。事实上,它不会发生在最新版本中。这是当前jQuery的代码(最有趣的部分是注释):
if ( typeof data === "string" ) {
try {
data = data === "true" ? true :
data === "false" ? false :
data === "null" ? null :
// Only convert to a number if it doesn't change the string
+data + "" === data ? +data :
rbrace.test( data ) ? jQuery.parseJSON( data ) :
data;
} catch( e ) {}
And it works in jQuery 1.8 and 1.9 : it doesn't convert the string to a number if a back conversion doesn't produce the same string. But it didn't work in jQuery 1.7.
它适用于jQuery 1.8和1.9:如果后向转换不产生相同的字符串,它不会将字符串转换为数字。但它在jQuery 1.7中不起作用。