我需要一个表单,可以在其中动态地将服务添加到契约(Contract)中,并且对于每项服务,我可以添加 1 个或多个(或无!)子输入。
例如我创建:
SERVICE-1
带有子输入:
选择:“选项 1”数量:“10”
选择:“选项 2”数量:“20”
选择:“选项 3”数量:“30”
SERVICE-2
带有子输入:
选择:“选项 1”数量:“40”
SERVICE-3
带有子输入:
选择:“选项 4”数量:“15”
选择:“选项 3”数量:“10”
和
SERVICE-4
没有子输入
我已将此 jsfiddle 编辑为我的示例:http://jsfiddle.net/WhaBx/22/
但我不知道如何传递这些分组输入以进入 insert.php
页面中的数组,例如:
$services =
[0] => array(
"name" => "SERVICE-1",
"attributes" => array(
[0] => array(
"attribute" => "Option 1",
"qty" => 10
),
[1] => array(
"attribute" => "Option 2",
"qty" => 20
),
[2] => array(
"attribute" => "Option 3",
"qty" => 30
)
)
),
[1] => array(
"name" => "SERVICE-2",
"attributes" => array(
[0] => array(
"attribute" => "Option 1",
"qty" => 40
)
)
),
[2] => array(
"name" => "SERVICE-3",
"attributes" => array(
[0] => array(
"attribute" => "Option 4",
"qty" => 15
),
[1] => array(
"attribute" => "Option 3",
"qty" => 10
)
)
),
[3] => array(
"name" => "SERVICE-4",
"attributes" => array(
)
)
最佳答案
您只需根据您想要的结果数组设置输入字段的名称。
这看起来类似于:
服务:
<input name="service[0][name]" value="Service-1" type="hidden">
<input name="service[1][name]" value="Service-2" type="hidden">
属性:
<input name="service[0][attributes][][attribute]" value="Option 1" type="hidden">
<input name="service[0][attributes][][qty]" value="10" type="hidden">
<input name="service[0][attributes][][attribute]" value="Option 2" type="hidden">
<input name="service[0][attributes][][qty]" value="20" type="hidden">
当然,您不会使用隐藏字段来放置您需要的输入类型。这只是一个示例,说明名称如何与您想要的值相对应。
关于javascript - 使用 php 检索与父输入相关的子输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48098199/