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 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog