2016-11-01 7 views
0
<div class="mt-repeater-item"> 
    <div class="row mt-repeater-row"> 
     <input type="text" name="product[0][price]" value="2" oninput="update_total(this)"> 
    </div> 
</div> 
<div class="mt-repeater-item"> 
    <div class="row mt-repeater-row">  
     <input type="text" name="product[1][price]" value="3" oninput="update_total(this)"> 
    </div> 
</div> 

Und mein SkriptFehler nicht erfasste Fehler: Syntaxfehler, nicht erkannte Äußerung

function update_total(input) { 
    var sum = 0; 
    $('.mt-repeater-item').each(function(index) { 
     var product_price = $("input[name=product["+index+"][price]]").val() ? $("input[name=product["+index+"][price]]").val() : 0; 
     sum = sum + product_price; 
    }); 
    alert(sum); 
} 

Error Uncaught Error: Syntax error, unrecognized expression => Wie es zu beheben?

Antwort

1

Sie haben Angebot für Name fehlt -

function update_total(input) { 
    var sum = 0; 
    $('.mt-repeater-item').each(function(index) { 
     var product_price = $("input[name='product["+index+"][price]']").val() ? $("input[name='product["+index+"][price]']").val() : 0; 
     sum = sum + product_price; 
    }); 
    alert(sum); 
} 

Verbindung - https://jsfiddle.net/80op2y19/

1
function update_total(input) { 
    var sum = 0; 
    $('.mt-repeater-item').each(function(index) { 
      var product_price = $("input[name='product["+index+"][price]']").val() ? $("input[name='product["+index+"][price]']").val() : 0; 
     sum = sum + parseInt(product_price); 
    }); 
    alert(sum); 
} 

enclose Wert von name in einfache Anführungszeichen

+1

auch verwenden 'parseInt'. Summe = Summe + ParseInt (Produktpreis); – Gowri

+0

@Gowri Der Code wurde geändert –

Verwandte Themen