Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> JavaScript

วิธีรักษา Leading 0 ไว้กับตัวเลข JavaScript


ไม่มีวิธีเก็บ leading 0 ไว้ในตัวเลข JavaScript อย่างไรก็ตาม คุณสามารถพิจารณา id นี้เป็นสตริงและส่งเป็นอาร์กิวเมนต์

ตัวอย่าง

คุณสามารถลองรันโค้ดต่อไปนี้เพื่อรักษา leading 0 โดยทำให้เป็นสตริง:

<html>    <head>       <script>          function sayHello(name, age, id) {             document.write (name + " is " + age + " years old, with id = "+id);          }       </script>    </head>    <body>       <p>Click the following button to call the function</p>       <form>          <input type="button" onclick="sayHello('John', 26, '0678')"             value="Display Employee Info">       </form>    </body> </html>