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 Spark加载本地pipelinemodel报错
  • ¥20 电脑鼠标键盘不好使了
  • ¥15 求糕手!用matlab求解目标规划模型🙏
  • ¥15 画出的分类图不对,求解答一下
  • ¥20 springboot和springcloud版本问题
  • ¥15 ps2手柄控制树莓派小车
  • ¥30 C#:vsto powerpoint的外接程序
  • ¥30 stata将do文件代码转化为ado文件
  • ¥15 两个同维数组相比,不同位置、出现重复比无意义,而不同位置、不出现重复比,则有意义。把有意义的两个数组放入新的集合MK中。
  • ¥15 写一个可直接调用的函数,将32位有符号数转成另一个无符号的数