简体   繁体   English

计算输入的值并将结果设置在另一个字段中

[英]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和两个输入字段,如XY
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.

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