我有一個簡單的按鈕$builder->add('language_switcher', ButtonType::class);
的形式,它應該只是,如果按下,添加另一個字段。要做到這一點,我用的Symfony的食譜http://symfony.com/doc/current/form/dynamic_form_modification.html按下按鈕後添加一個動態的表單字段
$builder
->get('language_switcher')
->addEventListener(
FormEvents::POST_SUBMIT,
function() use ($builder, $options) {
$preparedOptions = $this->prepareOptions($options['data']['productCustomizations']);
$builder->add('lang_switcher'), ChoiceType::class, $preparedOptions);
}
);
現在,當通過AJAX
在提交<script>
var $button = $('#xyz');
$button.click(function() {
var $form = $(this).closest('form');
$.ajax({
url: $form.attr('action'),
type: $form.attr('method'),
success: function(html) {
console.log(html);
$('#xyz').replaceWith($(html).find('#lang_switcher'));
}
});
});
</script>
,我發現了錯誤Buttons do not support event listeners.
於是,我嘗試過了一個隱藏字段。我加入了隱藏字段的形式設置EventListener
它,並添加這個data
我AJAX
要求
data[$('#id_of_hidden_field').attr('name')] = 1;
然而,這也沒做。該書中的例子是在提交選擇字段之後,因此我不知道如何使其適應我的需求。我無法使用SubmitType
,因爲它會提交表單,對吧?我只想用一個簡單的按鈕。
問題是,當我做一個console.log(html)
我看不到新的html元素,所以看起來好像我沒有去EventListener
這很奇怪,因爲如果我在監聽器I內部轉儲內容正在獲取一些數據。它只是好像我沒有得到它的迴應
有關事件偵聽器安裝到什麼(父)形式呢?這實際上觸發了這個事件。 – Rvanlaak