dqvs45976 2011-08-14 08:58
浏览 49
已采纳

使用循环从codeigniter中的视图中获取jquery函数的post数据

I have the following view in codeigniter- say edit_res_view-

<html>
<table>
    <? foreach ($list as $key => $value): ?>
        <tr><td><?= $key ?> :</td><td><input type="text" name='<?= $key ?>' size="25" value='<?php echo $value; ?>'/></td></tr>
    <? endforeach; ?>

    <tr><td colspan="2"><center><input type="submit" id="submit" size="40" value="SAVE" />

</table>

Now I have another jquery function in another view which takes the values from the name attributes of this form to send as POST data to another function -

$("#submit").live("click",function(){

    $.ajax({
        url: "http://localhost/codeigniter_local/index.php/manage_resources/doupdate_des",
        type:"POST",                                         
        data:"d_id="+$("[name='d_id']").val()+
              "&cpu_brand="+$("[name='cpu_brand']").val()+
              "&processor="+$("[name='processor']").val()+
              "&ram="+$("[name='ram']").val()+
              "&hdd="+$("[name='hdd']").val()+
              "&mac_id_wired="+$("[name='mac_id_wired']").val()+
              "&mac_id_wireless="+$("[name='mac_id_wireless']").val()+
              "&os="+$("[name='os']").val()+
              "&os_license_no="+$("[name='os_license_no']").val()+
              "&monitor_sno="+$("[name='monitor_sno']").val()+
              "&keyboard_sno="+$("[name='keyboard_sno']").val()+
              "&mouse_sno="+$("[name='mouse_sno']").val()+
              "&sno="+$("[name='sno']").val()+
              "&date_of_purchase="+$("[name='date_of_purchase']").val()+
              "&warranty="+$("[name='warranty']").val()+
              "&comments="+$("[name='comments']").val(),

        success:function(html){

            oTable.fnReloadAjax('http://localhost/codeigniter_local/index.php/manage_resources/json/1');
            alert("Done !");
            $("#form1").dialog("close");
        },  
        ERROR: function(html){                                             
            alert(html);                                        
        }  
    });
} );//for submit

Now the problem is that I want to loop through the values for the "data:" attribute of the jquery function instead of hardcoding it, as I have done above.

How to go about it? (btw the $list array is passed as an argument to edit_res_view through my controller).

  • 写回答

1条回答 默认 最新

  • doupu1957 2011-08-14 09:11
    关注

    Something like this should work using the jQuery map function:

    var postData = $("#form1 input:text").map ( function (J, node) {
        var jThis   = $(node);
        return jThis.attr ('name')  + '=' +  jThis.val ();
    } ).get ().join ('&');
    
    
    $.ajax({
        url:  "http://localhost/codeigniter_local/index.php/manage_resources/doupdate_des",
        type: "POST",                                         
        data: encodeURI (postData)
        ... ...
    


    That code assumes that the form has an id of "form1", and helps ensure that only the correct forms inputs are used.

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

报告相同问题?

悬赏问题

  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 lammps拉伸应力应变曲线分析
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题
  • ¥30 python代码,帮调试,帮帮忙吧
  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建