简体   繁体   English

如何防止我的程序使用 javascript 将以 0 开头的选定数字转换为八进制值?

[英]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.

相关问题 防止数字自动在JavaScript中将八进制转换为十进制? - prevent numbers from automatically converting octal to decimal in javascript? 如何防止javascript将我的日期转换为GMT? - How do I prevent javascript from converting my dates to GMT? JavaScript 中数字以零开头,不希望进行八进制转换 - Number Starts with Zero in JavaScript,Don't want for Octal Converstion 选择后,如何阻止我的javascript下拉列表显示空白框而不是所选值? - How can I stop my javascript dropdown from displaying blank box instead of selected value after selection? 如何使用 Javascript 或 Jquery 获取所选选项的值? - How can i get the value of the selected option using Javascript or Jquery? 如何使用javascript获取下拉选择值? - How can I get dropdown selected value using javascript? 如何让我的 Javascript 识别在我的 HTML 中选择了哪个单选按钮选项? - How can I get my Javascript to recognise which radio button option is selected in my HTML? 我如何忽略 javascript 将前导零转换为八进制? - How can i ignore the javascript convert leading zero to octal? 如何防止jQuery的$ .getJSON将我的ajax响应键从字符串转换为整数? - How can I prevent jQuery's $.getJSON from converting my ajax response keys from strings to integers? 未选择值时如何防止按钮提交表单。 不使用JavaScript - How to prevent buttons from submitting forms when value is not selected. Without using javascript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM