简体   繁体   English

在MVC中调用AJAX后,选择框的JavaScript文件不起作用

[英]JavaScript File for select box not work after AJAX call in MVC

I have a selectize.js JavaScript file in project that initially style the select box but when I load new select box via AJAX call not working this JavaScript.file for new appeared select box 我在项目中有一个selectize.js JavaScript文件,该文件最初为选择框设置样式,但是当我通过AJAX调用加载新选择框时,此JavaScript.file对于新出现的选择框不起作用

This is my AJAX code 这是我的AJAX代码

 $("#SlctGrpList").change(function (e) {
        e.preventDefault();
        $('#loadingIndicator').fadeIn();
        var curentUrl = "/search?SlctGrpFilt=" + $("#SlctGrpList").val() + getAllVars("SlctGrpFilt");
        $.ajax({
            url: "/Product/getOthFilter/" + $("#SlctGrpList").val(),
            success: function (result) {
                $('#OthFilter').html(result);
            },
        });
    });

SlctGrpFilt is a dropdown for product group AND when I change the value of group then OthFilter is a div and load with filtered select box related to the group of product and the name and id of select box is automatic with the number of filter now the filtered box can't be load perfectly because the styling script named selectize.js not working after AJAX call SlctGrpFilt是产品组的下拉列表,当我更改组的值时, OthFilter是div并加载与产品组相关的已过滤选择框,并且选择框的名称和ID是自动的,现在过滤器的数量已过滤框无法完美加载,因为名为selectize.js的样式脚本在AJAX调用后无法正常工作

This is an example of my filter box 这是我的过滤器框的一个例子

<select id="Filter_4" name="Filter_4" onchange="filterGroup(4)">
                    <option value="19"> option1</option>
                    <option value="21"> option2</option>
                    <option value="22"> option3</option>
                    <option value="23"> option4</option>
            </select>

after page load this select will turn to this 页面加载后,此选择将​​变为

    <div class="an-default-select-wrapper">
      <select tabindex="-1" class="selectized" style="display: none;">
        <option value="0" selected="selected">option1</option>
      </select>
      <div class="selectize-control single rtl">
         <div class="selectize-input items full has-options has-items">
            <div class="item" data-value="0">option1</div>
         <input type="text" autocomplete="off" tabindex="" style="width: 4px; opacity: 0; position: absolute; left: 10000px;"></div>
         <div class="selectize-dropdown single" style="display: none; visibility: visible; width: 190.5px; top: 40px; left: 0px;">
             <div class="selectize-dropdown-content"><div class="option selected" data-selectable="" data-value="0">option1</div>
         <div class="option" data-selectable="" data-value="1">option1</div>
         <div class="option" data-selectable="" data-value="2">option2</div>
         <div class="option" data-selectable="" data-value="3">option3</div>
         <div class="option" data-selectable="" data-value="4">option4</div>
       </div>
    </div>

What should I add to AJAX code that after succeed, all my automatic added select box styled with the selectize.js 我应该添加到AJAX代码中的什么,成功之后,所有自动添加的选择框都将使用selectize.js样式

Please re-design your code as below. 请如下重新设计您的代码。 I hope it will work. 我希望它能起作用。

$("#SlctGrpList").on("change", function (e) {
        e.preventDefault();
        $('#loadingIndicator').fadeIn();
        var curentUrl = "/search?SlctGrpFilt=" + $("#SlctGrpList").val() + 
getAllVars("SlctGrpFilt");
        $.ajax({
            url: "/Product/getOthFilter/" + $("#SlctGrpList").val(),
            success: function (result) {
                $('#OthFilter').html(result);
            },
        });
    });

This is because your new elements does not have event bound. 这是因为您的新元素没有事件绑定。 You should use the "on" method that provides you the option to bind events to elements created dynamically. 您应该使用“ on”方法,该方法为您提供了将事件绑定到动态创建的元素的选项。 Here is an example: 这是一个例子:

JQuery 1.7 and above jQuery 1.7及更高版本

$(useAnOuterStaticSelectorHere).on("change", "#SlctGrpList", function() {
    // code for event handling
});

You will find more examaples of this call in the jquery documentation: http://api.jquery.com/on/ 您将在jquery文档中找到此调用的更多示例: http ://api.jquery.com/on/

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

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