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");
    });
    
    评论

报告相同问题?

悬赏问题

  • ¥50 易语言把MYSQL数据库中的数据添加至组合框
  • ¥20 求数据集和代码#有偿答复
  • ¥15 关于下拉菜单选项关联的问题
  • ¥20 java-OJ-健康体检
  • ¥15 rs485的上拉下拉,不会对a-b<-200mv有影响吗,就是接受时,对判断逻辑0有影响吗
  • ¥15 使用phpstudy在云服务器上搭建个人网站
  • ¥15 应该如何判断含间隙的曲柄摇杆机构,轴与轴承是否发生了碰撞?
  • ¥15 vue3+express部署到nginx
  • ¥20 搭建pt1000三线制高精度测温电路
  • ¥15 使用Jdk8自带的算法,和Jdk11自带的加密结果会一样吗,不一样的话有什么解决方案,Jdk不能升级的情况