【发布时间】:2016-09-14 17:29:29
【问题描述】:
我有一个显示一些选项的 ChoiceType::Type 字段,我想为每个选项添加一个输入以在其上添加价格。我是这样做的:
->add('product_price', ChoiceType::class, array(
'choices' => array(
"Product 1",
"Product 2",
),
)
为每个选项添加输入的 JS:
var productBoxes = $("[id^=product_]");
// Listen the checkbox to display or hide the prices inputs
productBoxes.each(function (index) {
var priceField = '<label class="control-label required" for="product_price_' + index + '">Capacité</label>' +
'<input type="text" id="product_price_' + index + '" name="product[price][]" class="form-control">';
$(this).click(function () {
if ($(this).is(':checked')) {
$(this).parent().append(priceField);
}
})
})
javascript 可以工作,它会在每个选项旁边附加字段。现在我想将我的数据发送到一个数组中,如下所示: ["产品 1" => "附加字段的值"]
但我不知道如何获取这些额外数据并将其保存到数据库中。
有人知道怎么做吗?
编辑 1 我试图用 CollectionType 来做,但不知道如何将每个 CollectionType 元素呈现为复选框。有没有办法做到这一点?
感谢您的帮助!
【问题讨论】:
标签: php symfony symfony-forms