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条)

报告相同问题?

悬赏问题

  • ¥17 pro*C预编译“闪回查询”报错SCN不能识别
  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向