gpt4 book ai didi

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

转载 作者:搜寻专家 更新时间:2023-10-31 23:24:30 25 4
gpt4 key购买 nike

这是我第一次尝试制作 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com