【问题标题】:Using HTML input as function parameter causing infinite loop使用 HTML 输入作为函数参数导致无限循环
【发布时间】:2015-09-02 18:57:43
【问题描述】:

所以我用 JavaScript 制作了一个素数计算器,并试图在我的网站上显示它。我希望能够让用户选择输入,但是每当我提交输入时,它都会导致无限循环。我已将原因缩小到我用作函数参数的代码。

这是 HTML:

      <div class="pf-calculator-instructions-wrapper"><p class="pf-calculator-instructions">
        <em>Input a number to the field below. Output will appear above. Maximum 15 digits.</em></p>
      </div>
      <input id="pf-input" type="number" maxlength="15" class="pf-calculator-input"/>
      <button class="submit-button" onclick="findPrimeFactors(document.getElementById('pf-input').value)">Submit</button>

我已经尝试过了,所以这个函数只有一个数字(10)作为参数,它工作得很好,但是当它改变它以取输入值而不是(仍然是 10)时,页面就像我造成的一样冻结无限循环。我在这里遗漏了什么吗?

谢谢!

【问题讨论】:

  • 1) 如果没有你的 findPrimeFactors js,我们就无法调试它。 2)您是否将输入字符串值转换为数字?
  • 非常感谢!由于输入类型,我假设它已经是一个数字而不是一个字符串。

标签: javascript html css


【解决方案1】:

正如评论中提到的,我没有将输入从字符串转换为整数。由于输入类型是数字,我错误地认为它已经是这种格式。

【讨论】:

    猜你喜欢
    • 2012-07-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-01
    • 1970-01-01
    • 2019-11-01
    • 1970-01-01
    相关资源
    最近更新 更多