【问题标题】:How to continue receiving numbers until a zero is entered and count total positive and negative numbers the user entered.in the textfield如何在输入零之前继续接收数字并计算用户输入的正数和负数总数。在文本字段中
【发布时间】:2021-03-24 20:24:52
【问题描述】:

更多详情:

程序应该接收一个数字并继续接收数字[来自用户输入],直到 输入一个零。当输入一个零时,程序应该输出多少个正数以及如何 输入了很多负数,然后停止。

到目前为止,我只能在文本字段中一次输入 一个数字,并且只能输出一个正值或负值。

HTML

<!DOCTYPE html>
<html>
<head>
    <title>Exercise Two</title>
    <link href="ex2.css" rel="stylesheet">
    <script type="text/javascript" src="ex2.js"></script>
</head>
<body>

Enter number (Press 0 to stop)
<input type="text" id="num">
<button onclick="check()">Submit</button>

</body>
</html>

Javascript

function check(){

    let btnClear = document.querySelector('button');
    let inputs = document.querySelectorAll('input');
 
    btnClear.addEventListener('click', () => {
        inputs.forEach(input =>  input.value = '');
});

    var x = parseInt(document.getElementById("num").value);
    var posCount = 0;
    var negCount = 0;

    if (x > 0) {
        posCount++;
    }else{
        negCount++;
    }
    

    alert("Positive numbers: " + posCount + "\nNegative numbers: " + negCount);
}

【问题讨论】:

  • 当你想要它直到输入一个零时,为什么会有一个提交按钮?
  • 我想先输入一个随机数(不是 0)并提交它以清除文本字段。所以我可以输入一个新号码。
  • 可以通过javascript清除字段
  • 我将提交按钮放在那里,这样我就可以清除文本字段以输入新号码。一旦我输入了足够的随机数,我按 0 终止程序并计算已在文本字段中输入的所有正数和负数。
  • 是的,这是正确的,谢谢

标签: javascript html


【解决方案1】:

您可以尝试将数据附加到隐藏的input 元素:

function check() {

  let btnClear = document.querySelector('button');
  let inputs = document.querySelectorAll('input');

  btnClear.addEventListener('click', () => {
    inputs.forEach(input => input.value = '');
  });

  var x = parseInt(document.getElementById("num").value);

  var totals = document.querySelector("#totals");
  var posCount = totals.dataset.positives;
  var negCount = totals.dataset.negatives;

  if (x === 0) {
    alert("Positive numbers: " + posCount + "\nNegative numbers: " + negCount);
    totals.dataset.positives = 0;
    totals.dataset.negatives= 0;
  } else if (x > 0) {
    totals.dataset.positives = ++posCount;
  } else {
    totals.dataset.negatives= ++negCount;
  }
}
<p>Enter number (Press 0 to stop)</p>
<input type="text" id="num">
<input type="hidden" id="totals" data-positives='0' data-negatives='0'>
<button onclick="check()">Submit</button>

【讨论】:

    【解决方案2】:

    更新

    要实现这个,you can create a positive and negative variable outside的count函数然后检查每个输入值;如果输入的值为零,您可以调用警报函数并打印结果。

    这是工作代码:

    var posCount = 0;
    var negCount = 0;
    
    function printOutput () {
    alert("Positive numbers: " + posCount + "\nNegative numbers: " + negCount);
    
    }
    
    function check(){
    
        let btnClear = document.querySelector('button');
        let inputs = document.querySelectorAll('input');
     
        
    
        var x = parseInt(document.getElementById("num").value);
        document.getElementById("num").value = ''
    
        if (x > 0) {
            posCount++;
        }else if (x < 0){
            negCount++;
        } else {
          printOutput();
          // re-initiate value
          posCount = 0;
          negCount = 0;
        }
    
        
    }
    <!DOCTYPE html>
    <html>
    <head>
        <title>Exercise Two</title>
        <link href="ex2.css" rel="stylesheet">
        <script type="text/javascript" src="ex2.js"></script>
    </head>
    <body>
    
    Enter number (Press 0 to stop)
    <input type="text" id="num">
    <button id="submit-btn" onclick="check()">Submit</button>
    
    </body>
    </html>

    【讨论】:

    • 这个问题:如何在输入零之前继续接收数字并计算用户输入的正负数总数。在文本字段中
    猜你喜欢
    • 2022-11-30
    • 1970-01-01
    • 1970-01-01
    • 2022-09-26
    • 1970-01-01
    • 2018-04-01
    • 2012-03-06
    • 2014-12-08
    • 1970-01-01
    相关资源
    最近更新 更多