0

我正在尝试在 Jquery 中创建一个函数,该函数允许我将 3 个输入的值(包含以时间表示的数据)添加到第 4 个输入,如下所示:

INPUT13 INPUT12 INPUT11 INPUT14(结果)。

为此,我使用此功能:

var reg1 = /^(tiempo)(1)(1|2|3)$/;
var reg2 = /^(tiempo)(2)(1|2|3)$/;
var reg3 = /^(tiempo)(3)(1|2|3)$/;
    $("input[name^='tiempo']").bind( "blur", function() {
        //alert($(this).attr('name'))
        if ( $(this).attr('name').match(reg1) ){
            $("input[name^='totalt1']").val( $("input[name^='tiempo1']").sumValues() );
        }
        if($(this).attr('name').match(reg2)){
            $("input[name^='totalt2']").val( $("input[name^='tiempo2']").sumValues() );
        }
        if($(this).attr('name').match(reg3)){
            $("input[name^='totalt3']").val( $("input[name^='tiempo3']").sumValues() );
        }
        //$("input[name^='totalt']").val( $("input[name^='tiempo']").sumValues() );
    });

但是输入是动态的,因为用户可以创建多个,如下所示:

INPUTn1 INPUTn2 INPUTn3 INPUTn4(结果)。

进行更多输入的功能是:

var counter = 2;
    $("#addButton").click(function () {     
        if(counter>6){
            alert("Solo se permiten 6 Mediciones por dia");
            return false;}

        $('#tiempos_te').append( '<tr>' +
            '<td><input name="fac' + counter + '" type="text" id="fac' + counter + '" onKeyPress="return acceptNum(event)" maxlength="10" class="obligatorio"/></td>' +
            '<td> <input type="text"  name="tiempo' + counter + '1" id="tiempo' + counter + '1" class="obligatorio tiempo" maxlength="6" onKeyPress="return acceptNum(event)" /></td>' +
            '<td> <input type="text" name="tiempo' + counter + '2" id="tiempo' + counter + '2" class="obligatorio tiempo" maxlength="6" onKeyPress="return acceptNum(event)" /></td>' +
            '<td> <input type="text" name="tiempo' + counter + '3" id="tiempo' + counter + '3" class="obligatorio tiempo" maxlength="6" onKeyPress="return acceptNum(event)" /></td>' +
            '<td><input type="text" name="totalt' + counter + '" id="totalt' + counter + '" readonly="readonly" class="total_tiempo" /></td>' +
            '<td><select name="turn' + counter + '" id="turn' + counter + '" class="obligatorio">' +
            '<option value="2">Vespertino</option>' +
            '<option value="3">Nocturno</option>' +
            '</select></td>' +
          '</tr>' );        
        counter++;
    });

但是新的输入什么都不做,不执行求和,发生了什么?=(

4

1 回答 1

0

尝试

$('#tiempos_te').on('blur','input[name^="tiempo"]', function(){
   // since the names are in the format "tiempo#1" where # i sa number abd the last digit is either 1,2 or 3
   // you can extract the # number by removing the "tiempo" string and also the last digit.
   var number= this.name.replace('tiempo',''), // extract the number from tiempoxxx
       counter = number.substr(0, number.length -1); // keep everything from the number except the last digit 

   // now we are left with the counter as it was used to create these fields
   $('input[name^="totalt'+counter+'"]').val ( $('input[name^="tiempo'+counter+'"]').sumValues() );
});
于 2013-10-01T21:49:50.053 回答