[英]Form help: input - output for math function
我需要幫助。 我需要以下功能的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>
每當我按下按鈕時,頁面都會刷新,而我又回到了開頭。
非常感謝你!
使用Math.log()
為以e為底, Math.log10()
為基數10和Math.log2()
為基數2.注意log10
和log2
目前不支持在Internet Explorer
使用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);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.