我正在尝试比较两个变量值,但它没有按预期工作,我需要比较一个静态id(0000)四个零与用户输入的内容(userInput),我试图打印值但是它只打印第一个零并跳过休息,它也只比较用户值和第一个零,我需要将用户输入的值与精确的四个零进行比较 知道什么是错的吗?
function showProfile() {
var checkEmpId= document.getElementById("emp_id").value;
var staticEmpId=0000;
console.log(+checkEmpId); // from user Input
console.log(+staticEmpId); // shows only one 0
setTimeout(function(){
// I thought this will help ,still not working
if(parseInt(checkEmpId,4)== parseInt(staticEmpId,4)){
// I tried below as if statement but no luck
//if((document.getElementById("emp_id").value)="0000"){
console.log("Yay, Got luck"); }
else
{
console.log("else block is now clalled");
errorProfileId();
}
},4000)
}
<label>INCR</label><input type="text" class="form-control" name="otp" id="emp_id" required size="4" maxlength="4">
<button type="submit" class="btn btn-default btn-success" tabindex="-1" id="register_emp_btn" dismiss="modal" onclick="showProfile();">Compare</button>
funcion showProfile(){
var checkEmpId= document.getElementById("emp_id").value;
var staticEmpId=1234;
console.log(+checkEmpId);
console.log(+staticEmpId);
setTimeout(function(){
if(parseInt(checkEmpId,4)== parseInt(staticEmpId,4)){
// if((document.getElementById("emp_id").value)="0000"){
var checkID=document.getElementById("emp_id").value;
localStorage.setItem("LSEmpId",document.getElementById("emp_id").value);
window.location.replace("signup_step2.html");
}
else
{
console.log("else block is now clalled");
ErrorProfile();
}
},4000)
}
答案 0 :(得分:0)
在javascript(我甚至会说大多数编程语言)中,对整数的前导0被忽略,因此这就是你只得到一个0的原因。一个简单的解决方法是使用一个字符串作为HTTParty
做你的比较。希望这有帮助!
答案 1 :(得分:0)
function showProfile() {
var checkEmpId= document.getElementById("emp_id").value;
var staticEmpId="0000";
document.write("<br>"+checkEmpId); // from user Input
document.write("<br>"+staticEmpId); // shows only one 0
setTimeout(function()
{
if(checkEmpId===staticEmpId){
document.write("<br>Yay, Got luck");
}else{
document.write("<br>else block is now called");
}
},4000)
}
&#13;
<label>INCR</label><input type="text" class="form-control" name="otp" id="emp_id" required size="4" maxlength="4">
<button type="submit" class="btn btn-default btn-success" tabindex="-1" id="register_emp_btn" dismiss="modal" onclick="showProfile();">Compare</button>
&#13;