简体   繁体   中英

Ajax posting but object error

I am faccing posting issue. When I increase the total value with button then value are showing but when I click the request button then all posting are updated but balance do not update. And I check the database there insert into "You have transfer $[object Object] Account Balance". Please help what is the problem and help me for perfection.

 $(function(){ var theTotal = 10; $('.add').click(function(){ theTotal = Number(theTotal) + Number($(this).val()); $('.total').text(theTotal.toFixed(2)); }); $('.sub1').click(function(){ if(theTotal > 10) { theTotal = Number(theTotal) - Number($(this).val()); }; $('.total').text(theTotal.toFixed(2)); }); $('.sub2').click(function(){ if(theTotal > 10) { theTotal = Number(theTotal) - Number($(this).val()); }; $('.total').text(theTotal.toFixed(2)); }); $('.sub3').click(function(){ if(theTotal > 10) { theTotal = Number(theTotal) - Number($(this).val()); }; $('.total').text(theTotal.toFixed(2)); }); var af = $('.total').text(theTotal.toFixed(2)); $('#request').click(function(){ $.ajax({ type:'POST', url:window.location.protocol+'//'+window.location.host+'/?add=fund', data:'qun='+af, cache:false, beforeSend:function(){ $('html *').addClass('op-progress'); }, success:function(html){ $('html *').removeClass('op-progress'); document.location.href=_url+'/?add=fund'; } }); }); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.0/jquery.min.js"></script> <table style="margin-bottom:15px;"> <tr> <td> <button class="sub3 p_btn" value="10"><<<</button> <button class="sub2 p_btn" value="10"><<</button> <button class="sub1 p_btn" value="10"><</button> </td> <td> <span class="f_mb_dol">$</span> <span class="total f_mb_text"></span> <div class="f_mb_dol">usd</div> </td> <td> <button class="add p_btn" value="10">></button> <button class="add p_btn" value="10">>></button> <button class="add p_btn" value="10">>>></button> </td> </tr> </table> <table> <tr> <td><button id="request" class="btn_enable" style="cursor:pointer;"><span>Transfer balance</span></button></td> </tr> </table> 

You are sending the whole element in the data. I beleive you need to send the data only. What you are doing in line var af = $('.total').text(theTotal.toFixed(2)); is you are setting the value for that element and assigning the element to a variable.

Remember .text(something) sets the value

.text() gets the value

 $(function(){ var af; var theTotal = 10; $('.add').click(function(){ theTotal = Number(theTotal) + Number($(this).val()); $('.total').text(theTotal.toFixed(2)); }); $('.sub1').click(function(){ if(theTotal > 10) { theTotal = Number(theTotal) - Number($(this).val()); } $('.total').text(theTotal.toFixed(2)); }); $('.sub2').click(function(){ if(theTotal > 10) { theTotal = Number(theTotal) - Number($(this).val()); } $('.total').text(theTotal.toFixed(2)); }); $('.sub3').click(function(){ if(theTotal > 10) { theTotal = Number(theTotal) - Number($(this).val()); } $('.total').text(theTotal.toFixed(2)); }); $('.total').text(theTotal.toFixed(2)); $('#request').click(function(){ af = $('.total').text(); console.log(af); $.ajax({ type:'POST', url:window.location.protocol+'//'+window.location.host+'/?add=fund', data:'qun='+af, cache:false, beforeSend:function(){ $('html *').addClass('op-progress'); }, success:function(html){ $('html *').removeClass('op-progress'); document.location.href=_url+'/?add=fund'; } }); }); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.0/jquery.min.js"></script> <table style="margin-bottom:15px;"> <tr> <td> <button class="sub3 p_btn" value="10"><<<</button> <button class="sub2 p_btn" value="10"><<</button> <button class="sub1 p_btn" value="10"><</button> </td> <td> <span class="f_mb_dol">$</span> <span class="total f_mb_text"></span> <div class="f_mb_dol">usd</div> </td> <td> <button class="add p_btn" value="10">></button> <button class="add p_btn" value="10">>></button> <button class="add p_btn" value="10">>>></button> </td> </tr> </table> <table> <tr> <td><button id="request" class="btn_enable" style="cursor:pointer;"><span>Transfer balance</span></button></td> </tr> </table> 

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