简体   繁体   English

使用Ajax时通过Javascript设置ACF select2选项

[英]Set ACF select2 option via Javascript when using Ajax

I am trying to select an option in an Advanced Custom Fields select2 drop down that is lazy loaded by Ajax. 我试图在“高级自定义字段” select2下拉列表中选择一个由Ajax延迟加载的选项。 The new Javascript API is very powerful, but I can't figure out how to set an option when Ajax is in play. 新的Javascript API非常强大,但是当Ajax播放时,我不知道如何设置选项。

This works without Ajax, but not with: 这在没有Ajax的情况下有效,但不适用于:

var select = acf.getField('field_5c6ce2e0b5780');
select.select2.selectOption(7);

Anyone have any ideas how to make this work with Ajax? 任何人都对如何使Ajax发挥作用有任何想法?

After sifting through acf-input.js and also the Select2 documentation, I have found a way to do it using the addOption method. 在浏览了acf-input.js和Select2文档之后,我找到了一种使用addOption方法来完成此操作的方法。 I'm not sure if this is a workaround, but it is working for my situation. 我不确定这是否是一种解决方法,但它适合我的情况。

var select = acf.getField('field_5c6ce2e0b5780');
select.select2.addOption({
          id: 12345,
          text: 'New Option',
          selected: true
        });

This appears to be adding a new option to the list, but it doesn't override the option that is pulled from Ajax, so it works for my situation. 这似乎是向列表中添加了一个新选项,但它不会覆盖从Ajax提取的选项,因此适用于我的情况。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM