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 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵