简体   繁体   中英

js are not running on chrome but running on firefox

I have mention code and also dynamic code for textbox with js. i am facing problem in chrome while running this code but this same code is running on firefox properly, so please give me some suggestion regarding this code.

  1. html code for textbox

     <td><input class="form-control" required type='text' id='productname_1' name='productname[]'/></td> <td><input class="form-control" readonly="" required type='text' id='price_1' name='price[]'/></td> <td><input class="form-control" required type='text' id='quantity_1' name='quantity[]'/></td> <td><input class="form-control" readonly="" required type='text' id='total_1' name='total[]'/> </td> 
  2. Dymamic html code for textbox

     <td><input class='form-control productname12_"+i+"' required type='text' id='productname_"+i+"' name='productname[]'onchange='myFunction()'/></td> <td><input class='form-control' readonly type='text' id='price_"+i+"' required name='price[]'/></td><td><input class='form-control' type='text' required id='quantity_"+i+"' name='quantity[]'/></td><td><input class='form-control' readonly type='text' required id='total_"+i+"' name='total[]'/></td> 
  3. js code

     function myFunction() { var x = document.getElementById('productname_'+j).value; //alert(x); $.ajax({ type:"POST", url:"addplaceorder/getproductprice", data:{'name':x}, cache:false, success:function(html){ //alert(html); //alert('#price_'+k); $('#price_'+k).val(html); } }); $('#quantity_'+j).change(function(){ var val = $(this).val(); var price = $('#price_'+k).val(); //alert(name); var total = (val* price); //alert(total); totalamount = totalamount+total; //alert(totalamount); $('#total_'+k).val(total); $('#showtotal').text(totalamount); }); $('#quantity_'+j).keypress(function (e) { if (e.which != 8 && e.which != 0 && (e.which < 48 || e.which > 57)) { //display error message //$("#errmsg").html("Digits Only").show().fadeOut("slow"); return false; } }); j++;k++; } 

This might be the problem

var x = document.getElementById('productname_'+j).value;

The variable j is not initialized, therefore the value is undefined. I suppose that there is not element with id "productname_undefined". So when you try to read value from that undefined element, you will get an error. Something like "can not read property value of undefined"

PS: It is better to use the debugger statements, rather then alert() ;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM