jquery 未捕获的错误:在初始化之前不能调用模态上的方法;尝试呼叫方法'openModal'

fnatzsnv  于 2022-11-22  发布在  jQuery
关注(0)|答案(1)|浏览(116)

我在magento 2中使用javascript制作这个模型。它给出了这个错误。不知道是什么问题,它工作了几天。

<div id="custom-popup-modal">
<div class="col-sm-6">
<div class="static_content">
<h5 class="min-title-bold">JOIN THE PARTY</h5>
<h2 class="min-title-large">GET 15% OFF YOUR FIRST ORDER*</h2>
<p class="small-content">Subscribers only discounts, first look at new products, and cutting-edges tips</p>
{{block class="Magento\Newsletter\Block\Subscribe" name="static.newsletter" template="Magento_Newsletter::subscribe.phtml"}}
<p class="micro-text">*Promo code will be emailed to the address you enter, New subscribers only</p>
</div>
</div>
<div class="col-sm-6 img-section"><img src="{{media url="wysiwyg/image-email_1.gif"}}" alt="" /></div>
</div>
<script type="text/javascript" xml="space">// <![CDATA[
// 
// 
// 
    require(['jquery','Magento_Ui/js/modal/modal'],
        function($,modal) {
            var options = {
                type: 'popup',
                responsive: true,
            };
            var popup = modal(options, $('#custom-popup-modal'));
            $( document ).ready(function() {
                $('#custom-popup-modal').modal('openModal');
            });
        }
    );
// 
// 
// 
// ]]></script>
ruoxqz4g

ruoxqz4g1#

这可能是一个类似的问题:https://magento.stackexchange.com/questions/174029/cannot-call-methods-on-modal-prior-to-initialization-attempted-to-call-method

解决方案:

而不是在代码中使用以下代码:

var popup = modal(options, $('#custom-popup-modal'));
$( document ).ready(function() {
    $('#custom-popup-modal').modal('openModal');
});

使用此选项:

$( document ).ready(function() {
    $('#custom-popup-modal').modal(options).modal('openModal');
});

其他参考:jquery ui Dialog: cannot call methods on dialog prior to initialization

相关问题