基于另一个具有百分比的输入自动完成一个输入
Autocomplete an Input based on another Input with Percentages
首先,如果我遗漏了任何信息或不清楚,请原谅,我在这方面很新,仍在掌握窍门。
我有以下表格:
表单
我需要2%的输入字段来自动完成,所以总和是100%。如果输入1为90%,则输入2应自动完成到10%,反之亦然
这是HTML:
<!-- Tabla - - - - - - - -->
<div id="distDosFondos" class="table-responsive cambioDist">
<p>Selecciona fondos y porcentajes a distribuir:</p>
<!--table-responsive-->
<table class="table">
<thead>
<tr>
<th>Fondo</th>
<th>%</th>
</tr>
</thead>
<tbody>
<!-- Fila Cambio y Distribución de Fondos -->
<tr>
<td>
<div class="inputForm">
<select class="chosen" id="fondos1" name="fondos1">
<option value="">Fondo</option>
</select>
</div>
</td>
<td>
<div class="inputForm">
<input id="porcentage1" name="porcentage" type="text" class="form-control porcentage porcentaje1">
</div>
</td>
</tr>
<!-- //Fila Cambio y Distribución de Fondos -->
<!-- Fila Cambio y Distribución de Fondos -->
<tr>
<td>
<div class="inputForm">
<select class="chosen" id="fondos2" name="fondos2">
<option value="">Fondo</option>
</select>
</div>
</td>
<td>
<input id="porcentage2" name="porcentage" type="text" class="form-control porcentage porcentaje2">
</td>
</tr>
<!-- //Fila Cambio y Distribución de Fondos -->
</tbody>
</table>
</div>
<!-- Tabla - - - - - - - -->
有很多方法可以实现这一点,但这是一个非常简单的解决方案。
首先,为两个输入的input
事件设置一个处理程序
$("[name='porcentage']").on("input", function() {
...
});
现在,在其中,您将处理更改其他输入的问题。首先,让我们确保百分比不能超过100,也不能低于0。
if($(this).val() > 100)
$(this).val(100);
else if($(this).val() < 0)
$(this).val(0);
接下来,您需要获得另一个输入,即您想要更改的输入。
var otherInput = $(this).attr("id") == "porcentage1" ? $("#porcentage2") : $("#porcentage1");
最后,设置另一个输入的值,使两者相加为100。
$(otherInput).val(100 - $(this).val());
完整代码:
$("[name='porcentage']").on("input", function() {
if($(this).val() > 100)
$(this).val(100);
else if($(this).val() < 0)
$(this).val(0);
var otherInput = $(this).attr("id") == "porcentage1" ? $("#porcentage2") : $("#porcentage1");
$(otherInput).val(100 - $(this).val());
});
Fiddle
一种可能性是使用jQuery库:
$('#porcentage1, #porcentage2').on('input', function (event) {
var inputValue = parseInt($(this).val());
if (inputValue >= 0 && inputValue <= 100) { //ensure number is between 0 and 100
var otherValue = 100 - inputValue;
if (this.id === 'porcentage1') { //determines which input element was filled
$('#porcentage2').val(otherValue);
} else {
$('#porcentage1').val(otherValue);
}
}
})
您可能需要使用其他验证/检查来确保用户实际上正在输入一个数字等。
没有jQuery:
// attach a bunch of events to both inputs to handle any kind of change
['change', 'keyup', 'paste'].forEach(function(event) {
// .bind here essentially serves to pass reference of the current updated input, and the other input wich will be updated
porcentage1.addEventListener(event, onPercentageChange.bind(null, porcentage1, porcentage2));
porcentage2.addEventListener(event, onPercentageChange.bind(null, porcentage2, porcentage1));
});
function onPercentageChange(updatedInput, otherInput) {
var updatedValue = parseInt(updatedInput.value) || 0;
otherInput.value = 100 - updatedValue;
}
https://jsfiddle.net/tb7a7joc/
工作IE9+
相关文章:
- javascript函数,它接受两个输入:一个对象和一个键,并返回对象中该键的相应值
- 用户在文本框中输入一个数字,并根据使用 PHP 输入的数字重定向到另一个网页
- 如何要求用户输入一个介于 1 和 50 之间的整数,然后小程序打印出一行这个星数
- Javascript提示和警报输入一个数字,它将循环,您将输入数字以获得它的平均值
- 如果使用jquery输入一个字段,则自动填充其他字段
- 一直要一个数字,直到输入一个偶数
- 需要用户输入一个随机数,然后提醒用户数字 ibnputted
- 以 jQuery 为中心的文本框在 Safari 中输入一个字符后失去焦点
- 如何将我的 Google 地图功能从自动查找我的位置更改为让我输入一个位置
- 如何让两个不同的输入(一个用于移动设备,一个用于桌面)修改同一变量
- 允许html输入框只输入一个完整的数字和精确的十进制.5
- 我怎样才能使我的表单输入不返回任何结果,当它输入一个空查询
- 用TypeScript输入一个约束字典
- 只有在用户输入一个字符后,AngularJS才会调用数据库
- 在javascript中输入一个正斜杠
- 如何使移动
- 如何确保用户在Javascript中输入一个有效的数字
- Rails 3 -设计'请输入一个电子邮件地址'弹出消息,即使禁用了validatable
- 在输入中输入一个值
- 输入一个返回的可观察对象