开发者

Passing a numeric variable to jQuery .load

I am having a bit of a problem with the following:

function loadContent(id) {
     $("#featuredDiv").load("featuredDiv.php?list="+id);
     );
}

It all w开发者_JAVA百科orks fine when opening the URL with an ID of 21012011 like:

javascript:loadContent(28012011)

but when 04022011 is the id (javascript:loadContent(28012011)), it seems to change the number to 1057801.

I need the number to be passed through exactly as it is reading the page from a database. Does anyone have an idea what I am doing wrong?

Many thanks in advance!


That is because 04022011 is parsed by Javascript as an octal number.

Wrap the ID in quotes at all times and it will work.


The problem is the difference between decimal and octal notation.

For historical reasons, which have nothing to do with utility to modern programmers, a sequence of numbers starting with a 0 and containing no 8 or 9 characters is interpreted as an octal number. This means that it is treated as being in base-8, rather than base-10.

The octal 04022011 is 1057801 in decimal notation.

You can fix this by wrapping your number in quote marks (e.g. javascript:loadContent("28012011")): this will cause it to be interpreted as a string and passed directly into your URL.

However You appear to be using <a href="javascript:..."> links. This is bad practice. Look into an introduction to jQuery (e.g. jqfundamentals.com) to see optimal ways to bind actions to elements and events.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜