gpt4 book ai didi

javascript - 限制多个输入字段的总和值

转载 作者:行者123 更新时间:2023-11-30 10:31:42 24 4
gpt4 key购买 nike

我需要一些关于 javascript 或 jQuery 的帮助。

我要过滤三 (3) 个输入字段(三个字段的总和)。

3个输入框的总和必须是100,如果用户填写超过100,会自动改成总和为100。

可以看到this example

<input type="text" name="text1" size="3"> text1<br />
<input type="text" name="text2" size="3"> text2<br />
<input type="text" name="text3" size="3"> text3<br />

<p>The maximum value of total 3 fields above is 100.</p>
<pre>example 1 :

text1 : 20;
text2 : 30;
text3 : 50; (will automatically filled with 50 because the total value must 100)</pre>
<pre>example 2 :

text1 : 37;
text2 : 60;
text3 : 3; (will automatically filled with 3 because the total value must 100)</pre>

谢谢你帮助我,我需要它:)

最佳答案

$("input:lt(2)").on("change", function() {
var other = $("input:lt(2)").not(this).val();
if (other.length)
$("input:eq(2)").val(100 - this.value - other);
});

演示: http://jsfiddle.net/D5F3p/3/

关于javascript - 限制多个输入字段的总和值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16519334/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com