繁体   English   中英

未捕获的TypeError:无法调用未定义的方法“ split”

[英]Uncaught TypeError: Cannot call method 'split' of undefined

在这个小提琴有一个To button.When点击此按钮,从菜单中选择一个对话框appears.By默认users是selected.If我从这个用户表中选择一个复选框,如果变更后的下拉菜单组,然后从组表中选择一个复选框,然后Uncaught TypeError: Cannot call method 'split' of undefined出现在控制台中。但是,从一开始(不选择用户表的任何复选框)如果我更改为组表,然后选择一些复选框,则不会出现上述错误。任何机构可以告诉我如何解决此问题。

以下是jQuery代码

$('#ViewFull').click(function () {
    $('#mytable1 tr').removeClass('hide');
});
$('.select').on('change', function () {
    var tablink = '#' + $(this).val();
    $('#groups').hide();
    $('#users').hide();
    $(tablink).show();
});

function copy_users_table() {
    var users = $('#mytable').html();
    $('#mytable12').html(users);
}

function copy_groups_table() {
    var groups = $('#TogroupsTable').html();
    $('#groupsTable1').html(groups);
}

function collect_users_and_groups(is_groups) {
    var tos = [];
    $('#mytable12 input:checked, #groupsTable1 input:checked').each(function (i, elt) {
        //alert("to groups");
        if (is_groups) {
            //alert("to groups");
            var dataids = $(this).parent().attr("data-selected").split(",");
            alert("dataids  " + dataids);
        }
        var name = $.trim($(this).parent().next().text());
        tos.push(name);
    });

    return tos.join(', ');
}

$('body').on('click', '#to-btn', function () {
    // copy current tables
    copy_users_table();
    copy_groups_table();

    // initialize checkboxes
    var number = $('#number').val();
    $('#ToAdd').text(number);
    var entries = number.split(/\s*,\s*/);
    init_users_table(entries);
    init_groups_table(entries);

    $("#mytable12 input:checkbox").on('change', function () {
        var tos = collect_users_and_groups(false);
        $("#ToAdd").html(tos);
    });

    $("#groupsTable1 input:checkbox").on('change', function () {
        var tos = collect_users_and_groups(true);
        $("#ToAdd").html(tos);
    });

    // show tab
    $('.select').val('users');
    $('#users').show();
    $('#groups').hide();
});

$('#ToOk').click(function () {
    $("#number").val($("#ToAdd").text());
});

function init_users_table(entries) {
    // go through all rows
    $('#users tr').each(function () {
        var username = $('td:nth-child(2)', this).text();
        var selected = $.inArray(username, entries) >= 0;
        $('input:checkbox', this).prop('checked', selected);
    });
}

function init_groups_table(entries) {
    $('#groups tr').each(function () { //added 13
        var groupname = $.trim($('td:nth-child(2)', this).text());
        var selected = $.inArray(groupname, entries) >= 0;
        $('input:checkbox', this).prop('checked', selected);
    });
}

因此,当您调用“ collect_users_and_groups”函数时,代码同时针对属于#mytable12的已选中复选框和属于#groupsTable1的已选中复选框同时执行(由于要应用每个选择器)。 即使您使用is_groups = false调用该函数,代码仍会通过此代码段查找先前在#mytable12中选中的复选框:

if (is_groups) 
{
    //alert("to groups");
    var dataids = $(this).parent().attr("data-selected").split(",");
    alert("dataids  " + dataids);
}

因此,如果您先前在#mytable12中选中了一个复选框,则代码将为$(this).parent()。attr(“ data-selected”)返回undefined,因为此类表单元格没有此类属性。

通过以下方式更改代码:

$('#mytable12 input:checked').each(function (i, elt)
{
    var name2 = $.trim($(this).parent().next().text());
    tos.push(name2);
});
$('#groupsTable1 input:checked').each(function (i, elt) {
    //alert("to groups");
    if (is_groups) {
        //alert("to groups");
        var dataids = $(this).parent().attr("data-selected").split(",");
        alert("dataids  " + dataids);
    }
    var name = $.trim($(this).parent().next().text());
    tos.push(name);
});

暂无
暂无

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

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