【问题标题】:Dynamic Form with PHP and nested array input带有 PHP 和嵌套数组输入的动态表单
【发布时间】:2011-07-15 11:27:41
【问题描述】:

我有一个需要输入订单流程的项目。订单就像从下拉列表中选择一个项目一样简单。每个订单可能包含用于购买的多个行项目。据我了解,使用 JQuery 克隆此字段非常简单,我可以做到这一点。我被挂断的地方是,将有一组与为订单选择的项目相关的选项,这些选项需要绑定到该特定行项目。典型的订单可能类似于:

  • 产品项目:ASUS BASE DESKTOP
    • 选项
    • 4GB RAM 选项
    • Windows 7 x64 专业版
  • 产品项目:HP 底座笔记本电脑
    • 选项
    • 2GB RAM 选项
    • Windows Vista x32

我的 HTML 标记类似于:

<tr class="firstProduct productOrder">
    <td>Product Name</td>
    <td>
        <select id="ProductNameSelect" name="productOrdered[]">
            <option selected="selected" value="0">Select a Product...</option>
            <option value="1">ASUS BASE DESKTOP</option>
            <option value="2">HP BASE LAPTOP</option>
        </select>
    </td>
</tr>
<tr class="firstProduct productOrder">
    <td>Product Options</td>
    <td>
        <label>4GB RAM Option<input type="checkbox" name="productOption[0][]" value="1" /></label>
        <label>Windows 7 x64 Professional<input type="checkbox" name="productOption[0][]" value="2" /></label>
        <label>Windows Vista x32<input type="checkbox" name="productOption[0][]" value="3" /></label>
    </td>
</tr>

productOption 字段的名称必须命名为嵌套数组,否则为 productOrdered[1] 选择的选项可能无法正确分配。对于添加的每个订单,我需要能够将 productOption 的名称从 [0][ ] 到 [1][ ] 到 [2][ ] 等等,以便可以在PHP 的 POST 变量。

除了在 JQuery 中搜索前一个子项并解析名称以确定要在 productOption 名称中使用的下一个 ID 之外,是否有“最佳方法”或至少更常见的方法来实现此目标?

【问题讨论】:

    标签: php jquery forms clone


    【解决方案1】:

    是的,只需在 javascript 代码中声明一个全局变量,并在每次添加字段集时递增它。

    <script>
    var index=0;
    function addFieldset()
    {
       ...
       index++;
    }
    </script>
    

    【讨论】:

      猜你喜欢
      • 2022-06-15
      • 1970-01-01
      • 2021-09-06
      • 1970-01-01
      • 2017-07-01
      • 1970-01-01
      • 1970-01-01
      • 2019-08-13
      • 1970-01-01
      相关资源
      最近更新 更多