簡體   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