duanju7199 2013-08-13 23:27
浏览 45
已采纳

如何保存jquery动态生成的数据

I am using a math formula on my site to help users determine how many calories they must consume per day to lose weight, maintain current weight or build muscle. After they enter their personal stats they click click the "Generate Calorie Wallet" button and Jquery does the math and dynamically enters the data into three(3) empty cells each with separate class names depending on which of the 3 results they're looking fore. Here is the table below.

 <table>
    <tr>
        <td>Your Weight Loss Calorie Wallet</td>
        <td class="row_1"></td>
    </tr>
    <tr>
        <td>Your Maintanance Calorie Wallet</td>
        <td class="row_2"></td>
    </tr>
    <tr>
        <td>Your Muscle Gain Calorie Wallet</td>
        <td class="row_3"></td>
    </tr>
</table>
<form>
    <input type="button" class="save_wallet" value="Save To Profile"/>
</form>

The classes row_1, row_2, and row_3 are where the results are dynamically appearing from jquery and as you can see, there is a button below the table which will be used to let the user save the data into their profile in which I will use a simple HTTP Post request to carry out. The problem is when I run tests using alerts boxes I can't get jquery to pick up the dynamically generated data. I keep getting empty alert boxes.

Here is the code currently being used in jquery to calculate calorie wallet and insert into the rows and also the code used for the button to generate an alert.

 //Used for calculating the calorie wallet based on Katch Mcardle.
    $('.km_calculate').on("click",function(){
        var lbm = $(this).parent().children('.lbm').val();
        var activity = $(this).parent().children('.activity').val();
        var wt_unit = $(this).parent().children('.wt_unit').val();
        var bmr = 370 + (21.6 * lbm / wt_unit);
        var calorie_wallet = activity * bmr;
        var fatloss_wallet = calorie_wallet * .8;
        var muscle_gain = calorie_wallet * 1.2;
        $('.formula_2').slideUp();
        $('div#calorie_wallet').slideDown();
        $('.row_1').text(Math.round(fatloss_wallet));
        $('.row_2').text(Math.round(calorie_wallet));
        $('.row_3').text(Math.round(muscle_gain));
    });
    //Used to save the results from the math formulas
    $('.save_wallet').click(function(){
        var fatloss = $('.row_3').val();
            alert(fatloss);                              
    });

I hope some one can please help me with this, I have tried many attempts on my own by going to jquery.com but it's too much information to sift through and taking me too long to figure out on my own. Thank you all very much.

  • 写回答

1条回答 默认 最新

  • douqulv6059 2013-08-13 23:30
    关注

    Try using .text() or .html() instead of .val(), which is for inputs.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大
  • ¥15 import arcpy出现importing _arcgisscripting 找不到相关程序