简体   繁体   English

通过onclick获得价值

[英]Get Value With onclick

 var change = document.getElementById('pounds'); var click = document.getElementById('convert'); click.addEventListener('click', test(e)); function test(e) { change.addEventListener('input', function(e) { let lbs = e.target.value; document.getElementById('ounce').innerHTML = lbs * 16; }); } 
 <input type="text" placeholder='convert' id='pounds'> <p>Your conversion: </p> <p id='ounce'></p> <button id="convert" onclick="test()">Convert Now</button> 

I'm fairly new to JS and I'm learning by building simple stuff. 我是JS的新手,正在学习构建简单的东西。 I am trying to build a pounds to ounces converter, and I know how to get the real time value as I type but I want to get the result after I click a button. 我正在尝试构建磅到盎司转换器,并且我知道如何在键入时获取实时值,但是我想在单击按钮后获得结果。 My code kind of works.. You have to click the button before you type but that's not what I'm looking for. 我的代码工作正常。.在键入之前必须单击按钮,但这不是我想要的。 I want to get the value only by clicking the button. 我只想通过单击按钮来获取值。

I apologize if this a simple question but I can't seem to figure it out, haha. 如果这是一个简单的问题,我深表歉意,但我似乎无法弄清楚,哈哈。

I see a few things going on that I wanted to bring up. 我看到了一些我想提的事情。 First, below I'm going to attach a refactored version of the code and explain why I went about certain things the way I did. 首先,在下面,我将附加代码的重构版本,并解释为什么我要像以前那样处理某些事情。

 const conversionButton = document.getElementById('convert-button'); const conversionDisplay = document.getElementById('conversion-display'); const userInput = document.getElementById('pounds-input'); conversionButton.addEventListener('click', convertPoundsToOunces); function convertPoundsToOunces() { let lbs = userInput.value; conversionDisplay.innerHTML = lbs * 16; } 
 <input type="text" placeholder="convert" id="pounds-input"> <p>Your conversion: </p> <p id="conversion-display"></p> <button id="convert-button">Convert Now</button> 

The first thing I thought of was that you had the test function being executed inside the addEventListener method. 我想到的第一件事是您在addEventListener方法中执行了测试功能。 You want to reference the function there instead, so you would do 您想改用那里的功能,所以您可以

click.addEventListener('click', test);

instead of: 代替:

click.addEventListener('click', test());

or in this case with the new variable names: 或在这种情况下使用新的变量名称:

conversionButton.addEventListener('click', convertPoundsToOunces);

This way it is called at the time of the button being clicked. 这样,在单击按钮时调用它。

I noticed you were using the onclick attribute as well. 我注意到您也在使用onclick属性。 There is no need to have that there too. 那里也没有必要。 We simply want to add an event listener so our click handler function is ran when the button is clicked. 我们只想添加一个事件侦听器,以便在单击按钮时运行我们的单击处理程序功能。 We have access to the value of the input as well, so no need for e.target.value. 我们也可以访问输入的值,因此不需要e.target.value。 In this case userInput.value will pull the value inside the text input when convertPoundsToOunces is called. 在这种情况下userInput.value将拉动文本里面输入值时convertPoundsToOunces被调用。

Some last thoughts and suggestions: Change the variables up top to consts. 最后的一些想法和建议:将变量顶部更改为const。 The mixture of single quotes and double quotes inside the html attributes is a little weird. html属性中单引号和双引号的混合有点奇怪。 I generally stick with double quotes for html at all times. 我通常总是在HTML上使用双引号。 Lastly on the same note, I like naming my variables very semantic names. 最后,在同一条注释中,我喜欢使用非常语义的名称来命名变量。 Annnndddd finally think about how we could protect against when a user types in something besides a Number. 最后,Annnndddd考虑了当用户键入数字以外的内容时,如何防止出现这种情况。 Hope this was helpful! 希望这对您有所帮助!

Your second eventListener is listening for change to pounds. 您的第二个EventListener正在侦听磅的变化。 Just remove it: 只需将其删除:

 var change = document.getElementById('pounds'); var click = document.getElementById('convert'); click.addEventListener('click', test); function test(e) { let lbs = change.value; document.getElementById('ounce').innerHTML = lbs * 16; } 
 <input type="text" placeholder='convert' id='pounds'> <p>Your conversion: </p> <p id='ounce'></p> <button id="convert" onclick="test()">Convert Now</button> 

There are a couple of issues here. 这里有几个问题。 The main one is that you're executing the test function immediately rather than setting it as an event handler: 最主要的是您要立即执行test功能,而不是将其设置为事件处理程序:

click.addEventListener('click', test(e));

Instead of executing the function, just reference it: 无需执行功能,只需引用它即可:

click.addEventListener('click', test);

Additionally, inside the function you don't actually perform the calculation. 此外,在函数内部您实际上并未执行计算。 Instead, you're only setting the change handler to the text input. 相反,您仅将更改处理程序设置为文本输入。 (Which is why you need to click the button before the text starts updating.) Remove that handler and just invoke the logic directly: (这就是为什么您需要在文本开始更新之前单击按钮的原因。)删除该处理程序,然后直接调用逻辑:

function test(e) {
  let lbs = e.target.value;
  document.getElementById('ounce').innerHTML = lbs * 16;
}

Finally, in this case e isn't going to contain what you expect. 最后,在这种情况下, e不会包含您的期望。 But fortunately you don't need to use it at all anyway. 但是幸运的是,您根本不需要使用它。 You already have a reference to the element you need in the change variable: 您已经在change变量中引用了所需的元素:

function test() {
  let lbs = change.value;
  document.getElementById('ounce').innerHTML = lbs * 16;
}

Example

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

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