[英]Calculate the value of inputs and set the result in another field
I have a value 1000
and two input fields like X
and Y
.我有一个值
1000
和两个输入字段,如X
和Y
。
If I put a value 300
on X
then the value calculate with 1000
and automatically fill the Y
field with 700
.如果我将值
300
放在X
则该值会用1000
计算并自动用700
填充Y
字段。 Same as if I put the Y
field first then X
value will calculate automatically.就像我先放
Y
字段然后X
值将自动计算一样。 The value can be anything below 1000
.该值可以是低于
1000
任何值。 If it's over 1000
then I want to show a alert message.如果超过
1000
那么我想显示一条警报消息。
In the following code we create 2 inputs and we listen for a change event on both.在下面的代码中,我们创建了 2 个输入,并监听了两个输入的更改事件。 When one input value changes, we set the other's value to 1000 - value.
当一个输入值发生变化时,我们将另一个输入值设置为 1000 - value。
<p>
First
<input min="0" max="1000" type="number" id="first">
</p>
<p>
Second
<input min="0" max="1000" type="number" id="second">
</p>
Js (with jquery): Js(使用jquery):
$("#first").change((el)=>{
$("#second").val(1000 - el.target.value);
});
$("#second").change((el)=>{
$("#first").val(1000 - el.target.value);
});
https://codepen.io/Pocciox/pen/ExxzVXz https://codepen.io/Pocciox/pen/ExxzVXz
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.