weixin_33737774 2018-04-20 12:33 采纳率: 0%
浏览 25

jQuery关于更改AJAX问题

I have the following scenario:

I have a table that is populated with user data like name, surname, location and courses in a structure like the following:

<tr>                          
   <td data-label="First Name" class="wppb-name"><span>Mutanga</span></td>
   <td data-label="Last Name" class="wppb-name"><span>Eto Emule</span></td>         
   <td data-label="Location" class="wppb-name"><span>Somewhere</span></td> 
   <td data-label="Courses" class="wppb-name seminaria-multiselect">Course 1, Course 2, Course 3, Course 4, Course 5, Course 6</td>                    
</tr>

The courses are fetched in a long line of text with no separation which i dont want neither i can insert span elements manually to each course. I change it with the following jQuery snippet. This separates the courses and insert them into spans and it works.

$('.seminaria-multiselect').each(function() {                   
    $(this).html($(this).html().split(', ').map(function(el) {
        return '<span>' + el + '</span>';
    }));        
});

Now the problem is that when i apply filters (for example, filter the users for specific locations or specificcourses), it does the filtering using AJAX and it loses the 'span formatting' of the above snippet.

I understand that this can be solved using the on() method, something like this:

$(document).on('change', '.seminaria-multiselect', function() { 
    $('.seminaria-multiselect').each(function() {                   
        $(this).html($(this).html().split(', ').map(function(el) {
            return '<span>' + el + '</span>';
        }));        
    });
});

This snippet however does not work properly all by itself. When the page is loaded, the text is not separated into spans neither after applying filters.

If i keep both the snippets like so:

$('.seminaria-multiselect').each(function() {                   
    $(this).html($(this).html().split(', ').map(function(el) {
        return '<span>' + el + '</span>';
    }));        
});         
$(document).on('change', '.seminaria-multiselect', function() { 
    $('.seminaria-multiselect').each(function() {                   
        $(this).html($(this).html().split(', ').map(function(el) {
            return '<span>' + el + '</span>';
        }));        
    });
});

then it applies the span separation on page load but filtering still doesn't produce the result i want.

Q1) What should i correct in order to make it work? Q2) Is there a more efficient way to do this? Is it necessary to keep both snippets (the first one when the page loads and 2nd one for when it filters)

I thank you very much for your time :)

  • 写回答

1条回答 默认 最新

  • weixin_33701564 2018-04-20 12:40
    关注

    You can trigger on change event manually on page load in order to make it work.

    $(document).ready(function(){         
    $(document).on('change', '.seminaria-multiselect', function() { 
        $('.seminaria-multiselect').each(function() {                   
            $(this).html($(this).html().split(', ').map(function(el) {
                return '<span>' + el + '</span>';
            }));        
        });
    });
    $(".seminaria-multiselect").trigger("change");
    });
    
    评论

    报告相同问题?

    悬赏问题

    • ¥15 有人会思科模拟器嘛?
    • ¥30 遇到一个的问题,请教各位
    • ¥20 matlab报错,vflux计算潜流通量
    • ¥15 我该如何实现鼠标按下GUI按钮时就执行按钮里面的操作的方法
    • ¥15 关于#硬件工程#的问题:我这边有个锁相环电路没有效果
    • ¥15 20款 27寸imac苹果一体机装win10后,蓝牙耳机和音响放歌曲卡顿断断续续.
    • ¥15 VB.NET 父窗体调取子窗体报错
    • ¥15 python海龟作图如何改代码使其最后画出来的是一个镜像翻转的图形
    • ¥15 我不明白为什么c#微软的官方api浏览器为什么不支持函数说明的检索,有支持检索函数说明的工具吗?
    • ¥15 在我想检测ros是否成功安装时输入roscore出现以下