Javascript 使用keyup问题更改文本字段的值
我正在动态地将行插入到页面上。我要做的是计算所有的星期一输入数据,并将其显示在一个合计框中。数字可以以整数或.25、.50、.75的增量输入,如果在两者之间使用任何数字,它将向上取整 当我使用Javascript 使用keyup问题更改文本字段的值,javascript,jquery,Javascript,Jquery,我正在动态地将行插入到页面上。我要做的是计算所有的星期一输入数据,并将其显示在一个合计框中。数字可以以整数或.25、.50、.75的增量输入,如果在两者之间使用任何数字,它将向上取整 当我使用blur时,我可以使用它,但我真的希望它能在keyup上工作,所以它可以动态更新 我的问题是,如果我使用keyup,除非我按下delete键,否则该值将自动变回已更正的值。例如,键入2.23 在不完全重新编写代码的情况下,有没有一种方法可以让整个字段动态更新?下午的大部分时间我都在为这件事挠头。感谢您的帮助
blur
时,我可以使用它,但我真的希望它能在keyup
上工作,所以它可以动态更新
我的问题是,如果我使用keyup
,除非我按下delete键,否则该值将自动变回已更正的值。例如,键入2.23
在不完全重新编写代码的情况下,有没有一种方法可以让整个字段动态更新?下午的大部分时间我都在为这件事挠头。感谢您的帮助
$(document).on('keyup','.Monday',findTotalMon);
函数findTotalAll(){
var arr=document.getElementsByName('total');
var-tot=0;
对于(变量i=0;i
原始答案
我在移动设备上,但我要试着解释一下我的意思
$(document).on('keyup', '.Monday', findTotalMon);
function findTotalAll() {
var arr = document.getElementsByName('total');
var tot = 0;
for (var i = 0; i < arr.length; i++) {
if (parseFloat(arr[i].value))
tot += parseFloat(arr[i].value);
}
document.getElementById('totalHoursAll').value = tot;
if (tot === 0) {
document.getElementById('totalHoursAll').value = '';
}
}
function findTotalMon() {
var arr = document.getElementsByClassName('Monday');
var tot = 0;
for (var i = 0; i < arr.length; i++) {
if (parseFloat(arr[i].value)) {
var newValue = '';
newValue = (.25 * Math.round(4 * arr[i].value));
// Don’t change this value yet
// This is causing the input to change as you type
/* arr[i].value = newValue; */
// Instead you can create an html element next to the input
// And update the element with the new value
// After the user clicks out of the input you can then either update the input with the new value
// Or you can leave the input the way it is and keep the updated values beside the input
tot += parseFloat(newValue);
}
}
document.getElementById('totalHoursMon').value = tot;
if (tot === 0) {
document.getElementById('totalHoursMon').value = '';
}
findTotalAll();
}
$(document).on('keyup','.Monday',findTotalMon);
函数findTotalAll(){
var arr=document.getElementsByName('total');
var-tot=0;
对于(变量i=0;i
编辑的答案
这就是我在试图解释上述内容时的想法
$(文档).ready(函数(){
//在“.Monday”输入上设置事件侦听器
$(文件)。在('keyup','s.Monday',findTotalMon);
//设置事件侦听器以添加新输入
$('body')。在('click','addMore',addInput)上;
/**
*添加新输入
*/
函数addInput(){
//获取包含类为“monday group”的所有元素的数组
var arr=$(“.周一组”);
//获取数组长度
变量长度=arr.length;
//将长度设置为插入下一个数字
var inputIndex=长度;
//新输入的HTML代码
var newInput=''+
'' +
'' +
'';
//检查是否有5个字段或更少
//如果有5个字段,则隐藏“添加更多”按钮
如果(长度>=5){
$('#addMore').css('display','none');
返回false;
}
//插入新的输入字段
$(newInput).insertAfter(arr[length-1]);
}
/**
*总计
*/
函数findTotalAll(){
//获取包含“row total”类的所有输入的数组
var arr=$('.row total');
//设置总数=0
var-tot=0;
//循环遍历数组并将值添加到总计
对于(变量i=0;i
添加更多
你等到b怎么样