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 matlab(相关搜索:紧聚焦)
  • ¥15 基于51单片机的厨房煤气泄露检测报警系统设计
  • ¥15 路易威登官网 里边的参数逆向
  • ¥15 Arduino无法同时连接多个hx711模块,如何解决?
  • ¥50 需求一个up主付费课程
  • ¥20 模型在y分布之外的数据上预测能力不好如何解决
  • ¥15 processing提取音乐节奏
  • ¥15 gg加速器加速游戏时,提示不是x86架构
  • ¥15 python按要求编写程序
  • ¥15 Python输入字符串转化为列表排序具体见图,严格按照输入