duanchi0897 2013-05-16 00:29
浏览 44
已采纳

在Javascript或PHP中获取2d数组键值?

I have a table of in which all the cells have a textbox to edit cell data. Now I want to use ajax to save data to a table for each keyup event. I have each textbox named as a 2d array that contains 2 ids, 1 id for each key, that I want save in a table.

<input type="text" name="txtfield[1][2]" />

now those 1 and 2 keys are ids that I want to save each in a separate column in a table.

Is there an easy way of getting those key values or should I use some string manipulation to get those values?

Any suggestions either in javascript or php would help.

  • 写回答

3条回答 默认 最新

  • dongshang4984 2013-05-16 00:46
    关注

    It would probably be easier to name them

    <input type="text" name="txtfield-1-2" />
    

    Then, in PHP, you access them with

    for (i = 1; i <= m; i++)
        for (j = 1; j <= n; j++)
            whatever($_POST["txtfield-$i-$j"]); /* or $_GET[...] */
    

    In JavaScript, you do something like

    for (i = 1; i <= m; i++)
        for (j = 1; j <= n; j++)
            whatever(document.getElementById("txtfield-" + i + "-" + $j).value);
    

    I am assuming that these are in some kind of grid, so m and n are known. If not, Ek0nomik's solution would suit you better, although the regex should be \.*\[([0-9]+)\]\[([0-9]+)\] (\[ and \] should go outside of (...) and quantifier + should be added to allow indexes larger than 9) or, a bit more precise, ^txtfield\[([0-9]+)\]\[([0-9]+)\]$.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 R语言卸载之后无法重装,显示电脑存在下载某些较大二进制文件行为,怎么办
  • ¥15 java 的protected权限 ,问题在注释里