javascript - 如果它是第一个数字,如何删除零?

标签 javascript html

这是我第一次尝试制作 javascript,大约 2 周前我才开始学习,所以没有潜在的新事物。我基本上只是尝试将国际号码转换为实际需要调用的号码。

我需要弄清楚如何从 phonenumber 变量中删除前导零而没有其他零。我看到了涉及 parseInt 和其他解决方案的答案,但我不知道如何实现它们。

<script>
var countrycode = document.getElementById("country").value;
var phonenumber = document.getElementById("phnm").value;
function updatecountrycode() // Updates country code on user change //
 {countrycode = document.getElementById("country").value;}
function updatenumber() // Updates phone number on user input //
 {phonenumber = document.getElementById("phnm").value;}
function fullnumber() // Displays country code + phone number //
document.getElementById("output").innerHTML = 9011 + countrycode + phonenumber};
</script>
<form>
Choose the country you want to call
<select id="country" onchange="updatecountrycode();">
<option>Choose Country</option>
<option value="44" id="uk">United Kingdom</option>
<option value="353" id="ire">Ireland</option>
</select>
<br>
<br>
Enter Phone Number
<input type="text" id="phnm" onchange="updatenumber()"><br>
</form>
<button type="button" onclick="fullnumber()">complete number will display</button>
<p id="output">test</p>

最佳答案

一个简单的解决方案:

if (phonenumber.substr(0,1) == "0")
{
    phonenumber = phonenumber.substr(1);
}

关于javascript - 如果它是第一个数字,如何删除零?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33964757/

相关文章:

javascript - 将 FileAttachment 注释链接到文件

html - 导出的 MailChimp HTML 模板看起来不一样

javascript - 动态地将值传递给 href 属性

html - 雪佛龙按钮

javascript - Javascript 中的信用卡预测

javascript - 你能用用户定义的对象伪造 Array.isArray() 吗?

javascript - 重新渲染React js组件

javascript - 更改输入类型文件文本

html - 即使没有指定,网站也仅在 ios 上显示背景图像?

javascript - jQuery - 在其下方显示每个图像链接