[英]How can I prevent my program from converting selected number which starts with 0 to octal value using javascript?
When I select a number that starts with 0, such a number is converted to its octal form which I don't want but works perfectly well for those that didn't start with 0. Attached is pictures explaining what I mean.当我选择一个以 0 开头的数字时,这样的数字会转换为我不想要的八进制形式,但对于那些不以 0 开头的数字来说效果很好。附件是解释我的意思的图片。
Before Selection of Number starting with 0在选择以 0 开头的数字之前
After Selection of Number starting with 0选择从 0 开始的数字后
From the picture you can see 0703 got converted to 451. How can I stop this.从图片中您可以看到 0703 已转换为 451。我该如何阻止这种情况。
let phoneNumberSelector = document.querySelector("#phone"); const mtnNumber = ['703', '706', '803', '806', '810', '813', '814', '816', '903', '906', '913', '0703', '0706']; phoneNumberSelector.addEventListener('keyup', (e) => { removeElements(); for (let i of mtnNumber) { // i = i.toString(); if (i.startsWith(phoneNumberSelector.value) && phoneNumberSelector.value != '') { let listItem = document.createElement("li"); listItem.classList.add('list-items'); listItem.style.cursor = 'pointer'; listItem.setAttribute('onclick', "displayNames(" + i + ")") let word = "<b>" + i.slice(0, phoneNumberSelector.value.length) + "</b>"; word += i.slice(phoneNumberSelector.value.length); listItem.innerHTML = word; document.querySelector('.list').appendChild(listItem); } } }) function displayNames(param) { phoneNumberSelector.value = param; removeElements(); } function removeElements() { let items = document.querySelectorAll('.list-items') items.forEach((element) => { element.remove(); }); }
<section class="section1"></section> <section class="section2"> <div class="reg-form"> <form action="" method=""> <div class="form-container"> <h1 style="text-align: center;">Phonie</h1> <div class="input-reg email-input"> <label for="username">Username</label> <input id="username" type="text" name="username"> </div> <div class="list-div"> <ul class="list"></ul> </div> <div class="input-reg email-input"> <label for="phone">Phone Number</label> <input id="phone" type="text" name="phone"> </div> <div class="input-reg email-input"> <input class="submit-btn submit-btn2" type="submit" value="Check Number"> </div> </div> </form> </div> </section>
The numbers are getting converted to octal because you are passing the number (of type string) to the displayNames
function as a number (without quotes).这些数字正在转换为八进制,因为您将数字(字符串类型)作为数字(不带引号)传递给
displayNames
函数。
This is the offending line:这是违规行:
listItem.setAttribute('onclick', "displayNames(" + i + ")")
with the value of i
set to "0703"
, the value for onclick
becomes "displayNames(0703)"
.将
i
的值设置为"0703"
, onclick
的值变为"displayNames(0703)"
。
You can fix this by adding quotes around the i
variable in the string passed as onclick
.您可以通过在作为
onclick
传递的字符串中的i
变量周围添加引号来解决此问题。
For example:例如:
listItem.setAttribute('onclick', "displayNames('" + i + "')");
or with a string template to make it a bit easier to read:或使用字符串模板使其更易于阅读:
listItem.setAttribute('onclick', `displayNames("${i}")`);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.