【问题标题】:Form help: input - output for math function表单帮助:数学函数的输入 - 输出
【发布时间】:2015-07-26 17:15:27
【问题描述】:

我需要帮助。我需要以下函数的 javascript 代码:

n = 1 + (log(5156559813)/log(x))/log(3)

其中 n 是输出,x 是输入框。

如何让输入框变成x?我使用document.getElementById("x").innerHTML = 1 + (Math.log(5156559813) / Math.log(x)) / Math.log(3)吗?

我已经搜索过,但每次都会出现错误的情况。

这是我的代码:

<form class="uk-form">

 <fieldset data-uk-margin>

  <input type="text" placeholder="People in your group" id="x">

  <button class="uk-button uk-button-primary">Go!</button>

  <p id="number"></p>

 </fieldset>

 <script type="text/javascript">
  function n(x){
   return document.getElementById("number").innerHTML = 1 + (9.7123600597) / Math.log(x)) / 0.4771212547;
    document.getElementById('x').addEventListener('change', function () {
    document.getElementById('n').innerHTML = n(this.value);
   }, false);
   }
  </script>

 </form>

每当我点击按钮时,页面都会刷新,我又回到了开头。

非常感谢!

【问题讨论】:

    标签: javascript function input output logarithm


    【解决方案1】:

    Math.log() 用于基数 e,Math.log10() 用于基数 10,Math.log2() 用于基数 2。请注意,log10 和 log2 目前在 Internet Explorer 上是 not supported

    【讨论】:

      【解决方案2】:

      使用Math.log():

      function n(x)
      {
          return 1 + (Math.log(5156559813) / Math.log(x)) / Math.log(3);
      }
      

      x 是您的输入,n(x) 是您的输出。

      如果您希望每次输入值更改时都计算此函数,您可以附加一个事件侦听器:

      // As you stated in the commentes, x can be found in <input id="x">.
      document.getElementById('x').addEventListener('change', function () {
          // If your output goes into an <input>
          document.getElementById('n').value = n(this.value);
      
          // If your output goes into an <div>, <span> or another non self-closing HTML tag:
          document.getElementById('n').innerHTML = n(this.value);
      }, false);
      

      【讨论】:

      • 谢谢!我将如何编码我的输入框以将其“链接”到 x?我正在使用 html.
      • @jonahoflosh:你应该展示你的 HTML 代码,我猜不出你已经做了什么。
      • 到目前为止,这是我的代码.....我不知道如何继续。 &lt;script type="text/javascript"&gt;function n(x) { document.getElementById("number").innerHTML = 1 + (Math.log(5156559813) / Math.log(x)) / Math.log(3); } &lt;/script&gt; &lt;center&gt; &lt;form class="uk-form"&gt; &lt;fieldset data-uk-margin&gt; &lt;input type="text" placeholder="People in your group" id="x"&gt; &lt;button class="uk-button uk-button-primary"&gt;Go!&lt;/button&gt; &lt;/fieldset&gt; &lt;/form&gt; &lt;/center&gt;
      • @jonahoflosh:然后再检查我的答案。
      • 现在,当我点击按钮时,我的页面会刷新。我将在第一个问题中给出我的代码。
      猜你喜欢
      • 1970-01-01
      • 2011-03-24
      • 1970-01-01
      • 2023-03-26
      • 2011-11-24
      • 2016-10-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多