简体   繁体   English

使用KeyUp,如何在释放回车键后获取值?

[英]Using KeyUp, how do I get a value after the enter key is released?

I am trying to create a keyup event handler that checks the first text box so that when enter is released, it calculates the number hours worked by the user input. 我正在尝试创建一个检查第一个文本框的keyup事件处理程序,以便在释放enter时,它会计算用户输入的工作小时数。 If the user enters a number LESS THAN 40 it takes that input and multiplies it by $12, but if the user enters a number more than 40 (hours), it takes the extra hours and multiplies it by $18 and add $480 ($12 x 40 hours) and returns a value in the second text box. 如果用户输入的数字少于40,则需要输入并将其乘以12美元,但如果用户输入的数字超过40(小时),则需要额外的小时数乘以18美元并增加480美元(12美元x 40)小时)并在第二个文本框中返回一个值。 I believe I am just using the wrong operator or the wrong method used to get keyup for the enter key. 我相信我只是使用错误的运算符或用于获取Enter键的键盘的错误方法。 Thanks in advance 提前致谢

 let hours = document.getElementById("box1") let pay = document.getElementById("box2") document.addEventListener("keyup", press); function press() { if (event.key === "Enter") { let a = hours.value; if hours.value <= 40; let b = (a * 12); else hours.value >= 40; let c = [((a - 40) * 18) + 480] let c = pay.value = b; } 
 <p>Enter your hours:</p> <input type="number" id="box1"> <p>Your pay will appear here:</p> <input type="text" id="box2" disabled> 

Your JS has several syntax problems, such as mismatched braces, semi-colons in the wrong place and not receiving the event as an argument in to the press() function. 你的JS有几个语法问题,例如不匹配的大括号,错误位置的分号以及没有接收事件作为press()函数的参数。

The logic itself can be broken down in to two stages, one to work out the base earnings and one to work out earnings past the 40 hour level. 逻辑本身可以分为两个阶段,一个用于计算基本收益,另一个用于计算超过40小时水平的收益。 Also note that you need to be wary of invalid values being entered in to the field as, although it's a number field, it will still accept some alphabetic characters, such as e . 另请注意,您需要警惕输入到字段中的无效值,尽管它是一个数字字段,但它仍然会接受一些字母字符,例如e To work around that make sure to parseInt() any value you receive before working with it. 要解决这个问题,请确保在使用之前先收到parseInt()任何值。 Try this: 尝试这个:

 var hourBreak = 40; var baseRate = 12; var higherRateFactor = 1.5; let hourField = document.getElementById("box1") let payField = document.getElementById("box2") document.addEventListener('keyup', press); function press(e) { if (e.keyCode === 13) { var hours = parseInt(hourField.value, 10) || 0; var baseEarnings = Math.min(hours, hourBreak) * baseRate; var higherEarnings = Math.max(hours - hourBreak, 0) * (baseRate * higherRateFactor) payField.value = baseEarnings + higherEarnings; } } 
 <p>Enter your hours:</p> <input type="number" id="box1"> <p>Your pay will appear here:</p> <input type="text" id="box2" disabled> 

The formulas which you used in your code are good. 您在代码中使用的公式很好。 However your code have few problems: if-else statement cause js errors; 但是你的代码几乎没有问题:if-else语句导致js错误; you define let c twice which also cause error. 你定义let c两次,这也会导致错误。 You also miss event argument in press function. 您还会错过按功能中的事件参数。 In below snippet I reduce your if-else statement to correct b value if hours>40. 在下面的代码段中,如果小时> 40,我会减少你的if-else语句以纠正b值。 I also remove a variable. 我也删除a变量。

 let hours = document.getElementById("box1"); let pay = document.getElementById("box2"); document.addEventListener("keyup", press); function press(event){ if (event.key === "Enter") { let b = (hours.value * 12); if (hours.value > 40) { b = [((hours.value-40) * 18) + 480] } pay.value = b; } } 
 <p>Enter your hours:</p> <input type="number" id="box1"> <p>Your pay will appear here:</p> <input type="text" id="box2" disabled> 

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

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