简体   繁体   中英

Javascript Leading Zero Function Argument

I am scratching my head over a problem that I am having with one of my JavaScript functions.

I have the following code:

 function getdetails(id) 
{
$('#details').load('load.php?id='+id);
}

The issue with this is, the id often starts with a leading zero or a series of leading zero's, which means they get cut off. I understand that it's treating it as an octet which is fine.

So I try to add the following

function getdetails(id) 
{
var id = parseInt(id,10);
$('#details').load('load.php?id='+id);
}

This however still cuts off the zero's. I have seen some of the padding functions that people have suggested, but for one I can not predict how many zero's there will and as far as I can tell, the zero's get removed as soon as it gets passed through as an argument.

Any suggestion on how I can solve this problem?

Your code is fine - it's the calling code that needs to be changed. Rather than calling your method with an integer parameter:

getdetails(0011);

Consumers should call the method with a string parameter:

getdetails('0011');

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM