JavaScript if和else if语句无法正常运行

时间:2020-06-10 21:05:30

标签: javascript html forms if-statement verification

我在函数中有一个input type='number'和一个按钮,用于指示function testfun()和一个if语句,该语句用来验证数字是否在范围内并提供清晰的指令。

当我输入1-1000范围内的数字时,它可以正常工作。

当我输入小于1或> 1000的数字时,<div><p>不会填充。

HTML:

<div class="preT" id="preT">
  <form>
    <label for="voipLines">VoIP Lines</label>
    <input type="number" id="voipLines" name="voipLines" requiered min="1" max="1000" value="1">
  </form>
  <div class="voipbutton" id="voipbutton">
    <button class="button" onclick="testfun()">Test Clicker</button>
  </div>
</div>
<div id="duringT">
  <p Id="clickerTest"></p>
  <div Id="prBar" data-label=""></div>
  <div id="canvascon"></div>
</div>

JS:

function testfun() { //used in HTML
  var voip = document.getElementById("voipLines").value;
  if (voip < 1) { //voiplines verification 
    return document.getElementById("clickerTest").innerHTML = "<div><p>Make Sure VoIP Lines is within the range of 1 to 1000</p></div>";
  } else if (voip > 1000) {
    return document.getElementById("clickerTest").innerHTML = "<div><p>Make Sure VoIP Lines is within the range of 1 to 1000</p></div>";
  } else {
    var voipGuide = document.getElementById("clickerTest").innerHTML = '<h2>Clicker Successful!!!</h2>\
    <p>Please click the link below to start <b>' + voip + ' test</b></p>\
    <p>To change the amount of tests to run <a href= "">Click Here</a></p>';
  document.getElementById('preT').style.display = 'none';
    document.getElementById("canvascon").innerHTML = '<div id="canvas-container"><canvas id="canvs3" style="background-color:#F0F0F0; ">Your browser does not support the HTML5 canvas tag.</canvas><br><br></div>';
    voipsims();
    bootfun();
  }
}

4 个答案:

答案 0 :(得分:2)

在else正文中,您使用innerHTML,但在if和else中,您使用innerhtml。应该是innerHTML

答案 1 :(得分:2)

这是Codepen中的工作版本。 https://codepen.io/riza-khan/pen/YzwwRLQ?editors=1011

您的代码中存在一些不良做法。也就是说,最好先使用eventListeners,然后再通过HTML标签中的onclick调用函数。

第二,在拼写错误中有一些拼写错误。

通过更正,甚至不需要IF / ELSEIF块,但是我还是把它留在那里。

<div class="preT" id="preT">
    <form>
        <label for="voipLines">VoIP Lines</label>
        <input type="number" id="voipLines" name="voipLines" required min="1" max="1000" value="1">
    </form>
    <div class="voipbutton" id="voipbutton">
        <button class="button">Test Clicker</button>
    </div>
</div>
<div id="duringT">
    <p Id="clickerTest"></p>
</div>

document.querySelector(".button").addEventListener("click", () => {
    let voip = document.querySelector("#voipLines").value;
    if (voip < 1) {
        document.querySelector("#clickerTest").innerHTML =
            "<div><p>Make Sure VoIP Lines is within the range of 1 to 1000</p></div>";
    } else if (voip > 1000) {
        document.querySelector("#clickerTest").innerHTML =
            "<div><p>Make Sure VoIP Lines is within the range of 1 to 1000</p></div>";
    } else {
        var voipGuide = (document.getElementById("clickerTest").innerHTML =
            "<h2>Clicker Successful!!!</h2>\
    <p>Please click the link below to start <b>" +
            voip +
            ' test</b></p>\
    <p>To change the amount of tests to run <a href= "">Click Here</a></p>');
        document.getElementById("preT").style.display = "none";
        document.getElementById("canvascon").innerHTML =
            '<div id="canvas-container"><canvas id="canvs3" style="background-color:#F0F0F0; ">Your browser does not support the HTML5 canvas tag.</canvas><br><br></div>';
        voipsims();
        bootfun();
    }
});

答案 2 :(得分:1)

有一个拼写错误:该属性为.innerHTML,而不是.innerhtml。工作示例:

function testfun() { //used in HTML
  var voip = document.getElementById("voipLines").value,
      clickerTest = document.getElementById("clickerTest");
        
  if (voip < 1) //voiplines verification 
    return clickerTest.innerHTML = "<div><p>Make Sure VoIP Lines is within the range of 1 to 1000</p></div>";
  
  else if (voip > 1000)
    return clickerTest.innerHTML = "<div><p>Make Sure VoIP Lines is within the range of 1 to 1000</p></div>"; 
  
  else {
    
    var voipGuide = clickerTest.innerHTML = '<h2>Clicker Successful!!!</h2>\
    <p>Please click the link below to start <b>' + voip + ' test</b></p>\
    <p>To change the amount of tests to run <a href= '
    '>Click Here</a></p>';
    document.getElementById('preT').style.display = 'none';
    document.getElementById("canvascon").innerHTML = '<div id="canvas-container"><canvas id="canvs3" style="background-color:#F0F0F0; ">Your browser does not support the HTML5 canvas tag.</canvas><br><br></div>';
    
  }
}
<div class="preT" id="preT">
  <form>
    <label for="voipLines">VoIP Lines</label>
    <input type="number" id="voipLines" name="voipLines" min="1" max="1000" value="1" required>
  </form>
  <div class="voipbutton" id="voipbutton">
    <button class="button" onclick="testfun()">Test Clicker</button>
  </div>
</div>
<div id="duringT">
  <p id="clickerTest"></p>
  <div id="prBar" data-label=""></div>
  <div id="canvascon"></div>
</div>

答案 3 :(得分:0)

我不确定这个答案,但是我认为innerhtml应该是innerHTML,并且'if','else if','else'和“ {”和“}”之间不应有空格。所以应该是 “

如果(voip <1){

...

}否则,如果(voip> 1000){

...

}其他{

...

} “

在段落末尾也有一个\,请单击链接...我认为这是不正确的。