简体   繁体   English

AJAX成功后调用Javascript库

[英]Call Javascript Library after AJAX Success

I have a main page that uses an AJAX call to expose content from a second page depending on user selection. 我有一个主页,它使用AJAX调用根据用户选择从第二个页面公开内容。 One of the elements that is generated by the second page is a dropdown list. 第二页生成的元素之一是下拉列表。 My dropdown lists are formatted using a Javascript library. 我的下拉列表使用Javascript库格式化。

The issue I'm having is that, if I put the dropdown element on the main page, it renders with no issue at all. 我遇到的问题是,如果我将dropdown元素放在主页上,它将完全没有问题地呈现。 However, if it's rendered by the second page (via AJAX), the dropdown doesn't render. 但是,如果它是由第二页(通过AJAX)呈现的,则该下拉列表不会呈现。 I feel like I need to somehow call the library again upon success but I'm honestly not sure. 我觉得成功后需要以某种方式再次调用该库,但是老实说我不确定。

Here's the AJAX code from my main page: 这是我主页上的AJAX代码:

<script type="text/javascript">
    jQuery('input[name="location"]').click(function(){
        var data = {location : jQuery(this).val(), department : $('input[name="department"]:checked').val(), userID : 12345};
        jQuery.ajax({
            url: "/new-custom.php",
            type:'POST',
            data: data,
            dataType: 'html',
            async: 'true',
            success: function(result){
                jQuery('#div-custom').html(result).show(); 
            }
        });        
    });
</script>

Here's the form element rendered on new-custom.php: 这是在new-custom.php上呈现的form元素:

<div class="form-group">
    <label class="control-label" for="field_20">Room Type</label>
    <select class="form-control selectpicker" id="field_20" name="field_20">
        <option value="0" selected>Select an option</option>
        <option value="Double">Double</option>
        <option value="Queen">Queen</option>
        <option value="Standard">Standard</option>
        <option value="King">King</option>
    </select>
</div>

And here's the Javascript library that gets included on my main page: 这是我的主页中包含的Javascript库:

<script src="/vendors/bower_components/bootstrap-select/dist/js/bootstrap-select.js"></script>

Do I need to somehow execute the bootstrap-select.js library on the AJAX success function? 我是否需要以某种方式在AJAX成功函数上执行bootstrap-select.js库?

Many thanks in advance for any help! 在此先感谢您的帮助!

You need to initialize the plugin on the newly added elements after you add them. 添加新元素后,需要在新元素上初始化插件。

Change your code to : 将您的代码更改为:

        success: function(result){
            jQuery('#div-custom').html(result).show().find('.selectpicker').selectpicker(); // initialize new elements
        }

Yes. 是。 Most plugins, and all plugins that do immediate DOM manipulation, need to be initialized when elements exist. 当元素存在时,大多数插件以及所有可立即进行DOM操作的插件都需要初始化。 So if they are loaded by ajax then you need to initialize them within the ajax callback after they are inserted in DOM 因此,如果它们是由ajax加载的,则在将它们插入DOM之后,您需要在ajax回调中对其进行初始化

Can do something like: 可以做类似的事情:

jQuery('#div-custom')
      .html(result)
      .show()
      .find('.selectpicker')
      .selectPluginName(/* options*/);

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

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