简体   繁体   English

为什么这不会显示值?

[英]Why wont this display the value?

I want to use a value from a HTMl input but i cant make it into a variabel.我想使用来自 HTMl 输入的值,但我无法将其转换为变量。 Im a beginner at this so maybe it will be pretty obvious to you but i couldnt find anything on the internet that helped me with my problem.我是这方面的初学者,所以也许对你来说很明显,但我在互联网上找不到任何可以帮助我解决问题的东西。

script.js:脚本.js:

 function getInputValue(){ 
    var inputVal = document.getElementById("numberIn").value;  
    return inputVal;
}
document.getElementById('numberOut').innerHTML = getInputValue();

index.html:索引.html:

<input type="text" placeholder="Dezimalzahl hier eingeben" id="numberIn">
<button type="button" onclick="getInputValue();">Convertieren</button>

<p id="numberOut"></p> 
 
<script src="script.js"></script>

The reason it doesn't work for you is because the line document.getElementById('numberOut').innerHTML = getInputValue();它对你不起作用的原因是因为行document.getElementById('numberOut').innerHTML = getInputValue(); gets ran only once, when the input is empty.当输入为空时,只运行一次。 Your button's onclick only returns the value from the input, but it doesn't re-set it into numberOut .您的按钮的onclick仅返回输入的值,但不会将其重新设置为numberOut

Please look at my example, I took the same exact line and put it into a function that a button executes, and it seems to work perfectly!请看我的例子,我把同样的行放在一个按钮执行的 function 中,它似乎工作得很好!

 function getInputValue() { const inputValue = document.getElementById("myInput").value; return inputValue; } function showValue() { document.getElementById("result").innerHTML = getInputValue() }
 <input type="text" id="myInput"/> <div id="result"></div> <button onclick="showValue()">Click Me!</button>

Your idea is你的想法是

When I click on the button, that's what should happen:当我单击按钮时,应该会发生这种情况:

  1. Read the value for an input element from the DOM.从 DOM 中读取输入元素的值。
  2. Assign that value to another element.将该值分配给另一个元素。

but, in your implementation you did但是,在您的实施中,您做到了

When I click on the button:当我点击按钮时:

  1. Read the value.读取值。
  2. Return it.把它返还。

Believe me, that's what your implementation is saying.相信我,这就是您的实施所说的。

If we came to the last step如果我们来到最后一步

document.getElementById('numberOut').innerHTML = getInputValue();

He's calling you, "Hey, why you are putting me outside the function, we did not agree like that".他在打电话给你,“嘿,你为什么要把我放在 function 之外,我们不同意那样的”。

So, that is the first correction所以,这是第一次更正

Let deal with the assignment function and place it inside the function.让我们处理分配 function 并将其放在 function 中。

The second, and after placed the assignment function in his reserved place... the function now has the full steps第二个,在将分配 function 放在他的保留位置之后...... function 现在有完整的步骤

  1. Read the value.读取值。
  2. ... ... ... …………

Assign it to the other element directly:), there is nothing that my function should return.直接将它分配给另一个元素:),我的 function 应该返回什么。

And, now!现在!

Should I write the final getInputValue() function, or I let it to you?我应该写最后的getInputValue() function,还是我让给你?

Greetings;)问候;)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM