2015-01-20 3 views
0

저는 jquery에서 새로운 프로그래밍이고 jquery 함수가 꺼내지면 웹 페이지에 문제가 있습니다. 어떤 테스트 어떤 제품은 열 'Unidad 맥시멈'과 'Unidad 최소값'변화를 선택 www.nutrinet.hol.es/index6.phpJquery 함수가 모든 열을 업데이트하지 않습니다.

을 수행하려는 경우

이 내 웹 페이지입니다.

다른 제품을 선택하면 (예 : 3 번째) 해당 행만 변경되고 다음 행은 변경되고 다른 행 (2 번째 및 1 번째)은 아직 업데이트되지 않습니다.

이것은 내 jquery 기능입니다. Val3 = 'Unidades elegidas'열 필드.

$(document).ready(function() { 



    $(".txtMult input").keyup(multInputs); 

    function multInputs() { 

     var mult = 0; 
     var sum = 0; 
     var cantidadfinal = 0; 
     var preciofinal =0; 
     var cantidadesminimas = 0; 

       var cantidadesmaximas = 0; 


     // for each row: 
     $("tr.txtMult").each(function() { 


      // get the values from this row: 
      var $val1 = $('.val1', this).val(); 
      var $val2 = $('.val2', this).val(); 
      var $val3 = $('.val3', this).val(); 
      var $val4 = $('.val4', this).val(); 



      var $total = ($val1 * 1) * ($val2 * 1) 
      var $cantidadestotales = ($val3 * 1) * ($val2 * 1) 
      var $preciostotales = ($val4 * 1) * ($val2 * 1)   


      mult += $total; 

      cantidadfinal += $cantidadestotales; 

      preciofinal += $preciostotales; 

      sum += parseFloat($val2); 

      cantidadesminimas = (8260 - cantidadfinal)/$val3 

      cantidadesmaximas = (12390 - cantidadfinal)/$val3 


      $('.val5',this).html(cantidadesminimas.toFixed(0)); 

       $('.val6',this).html(cantidadesmaximas.toFixed(0)); 



     }); 

     $("#precio").html(preciofinal.toFixed(0)); 
     $("#cantidadelegida").html(cantidadfinal.toFixed(0)); 
     $("#sum").html(sum.toFixed(0)); 
     $("#grandTotal").html(mult.toFixed(0)); 
     $("#cantidadmin").html(8260-cantidadfinal.toFixed(0)); 
     $("#cantidadmax").html(12390-cantidadfinal.toFixed(0)); 
     $("#kcaltotal").html(14000-mult.toFixed(0)); 

    } 

});

모든 댓글은 매우 감사하겠습니다. 감사합니다.

+0

입력을 변경할 때 둘 이상의 행이 업데이트되는 것이 예상되는 동작입니까? – acontell

답변

0

좋습니다. 변수 cantidadesminimascantidadesmaximascantidadfinal의 값에 따라 달라지며 cantidadfinal의 유효한 값을 알지 못합니다. 모두 행 행을 거쳐야합니다. 이로 인해 행 x이 행 y 다음에 오는 경우 cantidadfinal은 행 y의 값을 고려하지 않으므로 오류가 발생합니다. 가장 빠른 해결책은 루프를 두 번 반복하는 것입니다. 먼저 cantidadfinal을 계산하고 다른 루프는 .val5.val6에 올바른 값을 설정합니다. 내가 당신을 추천합니다 다음에 대한

$(".txtMult input").keyup(multInputs); 
function multInputs() { 
    var mult = 0; 
    var sum = 0; 
    var cantidadfinal = 0; 
    var preciofinal = 0; 
    var cantidadesminimas = 0; 
    var cantidadesmaximas = 0; 

    // for each row: 
    $("tr.txtMult").each(function() { 
     // get the values from this row: 
     var $val1 = $('.val1', this).val(); 
     var $val2 = $('.val2', this).val(); 
     var $val3 = $('.val3', this).val(); 
     var $val4 = $('.val4', this).val(); 

     var $total = ($val1 * 1) * ($val2 * 1); 
     var $cantidadestotales = ($val3 * 1) * ($val2 * 1); 
     var $preciostotales = ($val4 * 1) * ($val2 * 1); 

     mult += $total; 
     cantidadfinal += $cantidadestotales; 
     preciofinal += $preciostotales; 
     sum += parseFloat($val2); 
    }); 

    // Now we have the correct cantidadfinal and can update the values properly 
    $("tr.txtMult").each(function() { 
     var $val3 = $('.val3', this).val(); 
     cantidadesminimas = (8260 - cantidadfinal)/$val3; 
     cantidadesmaximas = (12390 - cantidadfinal)/$val3; 

     $('.val5', this).html(cantidadesminimas.toFixed(0)); 
     $('.val6', this).html(cantidadesmaximas.toFixed(0)); 
    }); 

    $("#precio").html(preciofinal.toFixed(0)); 
    $("#cantidadelegida").html(cantidadfinal.toFixed(0)); 
    $("#sum").html(sum.toFixed(0)); 
    $("#grandTotal").html(mult.toFixed(0)); 
    $("#cantidadmin").html(8260 - cantidadfinal.toFixed(0)); 
    $("#cantidadmax").html(12390 - cantidadfinal.toFixed(0)); 
    $("#kcaltotal").html(14000 - mult.toFixed(0)); 

} 

는 사물의 종류가 정말 좋은 쉽게 수행, 녹아웃라는 아주 좋은 자바 스크립트 플러그인을 사용합니다.

당신이 fiddle

에 결과를 확인할 수는 도움이되기를 바랍니다.

+0

도움을 주셔서 대단히 감사합니다. 사용자가 제품의 수량을 지속적으로 변경함으로써 'Cantidad minima que queda'값과 관련하여 각 제품의 최소값을 확인해야하기 때문에 모든 필드를 업데이트해야합니다. 그건 모든 필드를 업데이 트하는 이유지만, 만약 내가 2 번 제품의 수량을 5 번 수정할 경우, 다른 필드가 아무런 문제없이 업데이트 될 때 1 번 필드가 아직 업데이트되지 않은 이유를 모르겠습니다. – user3436868

+0

@ user3436868 명확한 설명을 주셔서 감사합니다. 실수였습니다. 문제를 오해했습니다. 나는 해결책을 생각해 내면, 한 번해볼 게요! – acontell

+0

내 문제에 대한 나쁜 설명에 죄송합니다. 대단히 감사합니다. 나는 이것을 해결하려고 노력할 것이다. 좋은 하루 되세요! – user3436868

관련 문제