来自一个输入的两个参数。 JavaScript

问题描述 投票:0回答:2

我正在执行JS练习。任务是:编写JavaScript程序,检查两个给定的整数值是否在50..99(含)范围内。如果其中一个在上述范围内,则返回true。任务很容易,我从两个输入中提取了两个数字。是否可以仅从一个输入取两个数字?并启动这样的功能:

function task28(fnum, snum){}

下面是具有两个输入的地雷解决方案。

<input type="text" id="task28a" class="form-control" placeholder="write number" aria-label=""
                        aria-describedby="basic-addon2">
<input type="text" id="task28b" class="form-control" placeholder="write number" aria-label=""
                        aria-describedby="basic-addon2">
  </br>
<button type="button" class="btn btn-dark btn-sm" onclick="task28()">Check</button>
<p class="answer" id="task28ans"></p>
<script>
function task28() {
    let fnum = document.getElementById("task28a").value;
    let snum = document.getElementById("task28b").value;
    if (
        (fnum >= 50 && fnum <= 99) && (snum >= 50 && snum <= 99)
    ) {
        document.getElementById("task28ans").innerHTML = "true";
    } else {
        document.getElementById("task28ans").innerHTML = "false";
    }
}
</script>

javascript button input onclick
2个回答
0
投票

我建议您使用表单并在提交时运行功能。这也可以简化检索您的值

function DoSubmit(){
  var a = document.myform.myinput.value;
  var b = document.myform.message.value;
  console.log(a,b) 
}
<form name="myform" onsubmit="DoSubmit();">
<input type="text" name="myinput" value="" />
<input type="text" name="message" value="" />
<input type="submit" name="submit" />
</form>

0
投票

检查下面的代码。...

function task28() {
    let tempVar = document.getElementById("task28a").value;
    let splitArray = tempVar.split(" ");
    if(splitArray.length == 2)
    {
      let fnum = parseInt(splitArray[0]);
      let snum = parseInt(splitArray[1]);
      if ((fnum >= 50 && fnum <= 99) && (snum >= 50 && snum <= 99)) {
            document.getElementById("task28ans").innerHTML = "true";
        } else {
            document.getElementById("task28ans").innerHTML = "false";
        }
    }
    
}
<input type="text" id="task28a" class="form-control" placeholder="Enter number" aria-label="" aria-describedby="basic-addon2">

  </br>
<button type="button" class="btn btn-dark btn-sm" onclick="task28()">Check</button>
<p class="answer" id="task28ans"></p>
© www.soinside.com 2019 - 2024. All rights reserved.