.JOIN JQuery错误消息显示在按钮上单击

时间:2019-02-18 13:38:39

标签: javascript jquery

我有一个表格,上面有星期几按钮,然后单击按钮,将选定的按钮传递给确认modal,我一切正常。我遇到的问题是,如果我然后仅关闭modal,填充字段,然后尝试重新提交,我总是收到错误

  

.join不是函数

JQuery

var selectedWkDays = [ ];
var selectedMthDays = [ ];
var selectedMths = [ ];

$('#submitButton').click(function () {
    getSelectedWeekdayValues();
    getSelectedDaysOfMonthValues();
    getSelectedMonthValues();
    $("b[name = 'modalDialPlanTelNo']").html($('#dialPlanTelNo').html());

    if (pageState == 'Inactive' && buttonclicked == 'Add') {
        $('#inactiveAddSuccessModal').modal();
        addModifyRuleFieldSelections();

        // Sets focus to 'Add More Rules' button once modal loaded
        $('#inactiveAddSuccessModal').on('shown.bs.modal', function () {
            $('#addMoreRulesButton').focus();
        })
    } else if (pageState == 'Active') {
        $('#activeAddSuccessModal').modal();

        if (buttonclicked == 'Add') {
            $("#addedupdatedtext").html('added');
            $('#addedLastSentence').css("display", "block");
            $('#addAnotherRuleButton').css("display", "inline");
        } else {
            $("#addedupdatedtext").html('updated');
            $('#addedLastSentence, #addAnotherRuleButton').css("display", "none");
        }

        addModifyRuleFieldSelections();

        // Sets focus to 'Close' button once modal loaded
        $('#activeAddSuccessModal').on('shown.bs.modal', function () {
            $('#modalCloseButton').focus();
        })
    }
});

// Creates an array for the selected weekdays to pass to modal
function getSelectedWeekdayValues(){            
    /* looks for all weekday buttons and check if it was selected */
    $("#selectWeekdaysSection .btn-primary").each(function() {
        selectedWkDays.push($(this).val());
    });

    /* joins the array which is separated by the comma */
    var wkDaysSelected;
    wkDaysSelected = selectedWkDays.join(', ');

    /* checks if there is selected buttons, by default the length is 1 as it contains one single comma */
    if(wkDaysSelected.length > 0){
        /* converts the wording if all weekday buttons selected */
        if (wkDaysSelected == "Mon, Tue, Wed, Thur, Fri, Sat, Sun") {
            wkDaysSelected = "All days";
        } else if (wkDaysSelected == "Mon, Tue, Wed, Thur, Fri") {
            wkDaysSelected = "Weekdays only";
        } else if(wkDaysSelected == "Sat, Sun") {
            wkDaysSelected = "Weekends only";
        } else {
            wkDaysSelected = wkDaysSelected;
        }
    } else {
        /* if no days where selected, sets the default to 'All days' */ 
        wkDaysSelected = "All days";
    }

    selectedWkDays = wkDaysSelected;
}

// Ccreates an array for the selected days of the month to pass to modal
function getSelectedDaysOfMonthValues(){            
    /* looks for all days of the month buttons and check if it was selected */
    $("#selectDaysOfMonthsSection .btn-primary").each(function() {
        selectedMthDays.push($(this).val());
    });

    /* joins the array which is separated by the comma */
    var mthDaysSelected;
    mthDaysSelected = selectedMthDays.join(', ') ;

    /* checks if there is selected buttons, by default the length is 1 as it contains one single comma */
    if(mthDaysSelected.length > 0){
        /* converts the wording if all days of the month buttons selected */
        if(mthDaysSelected == "01, 02, 03, 04, 05, 06, 07, 08, 09, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31") {
            mthDaysSelected = "All days of the month";
        } else {
            mthDaysSelected = mthDaysSelected;
        }
    } else {
        /* if no days where selected, sets the default to 'All days of the month' */ 
        mthDaysSelected = "All days of the month";
    }

    selectedMthDays = mthDaysSelected;
}

// Creates an array for the selected months to pass to modal
function getSelectedMonthValues(){            
    /* looks for all days of the month buttons and check if it was selected */
    $("#selectMonthsSection .btn-primary").each(function() {
        selectedMths.push($(this).val());
    });

    /* joins the array which is separated by the comma */
    var mthsSelected;
    mthsSelected = selectedMths.join(', ') ;

    /* checks if there is selected buttons, by default the length is 1 as it contains one single comma */
    if(mthsSelected.length > 0){
        /* converts the wording if all days of the month buttons selected */
        if(mthsSelected == "Jan, Feb, Mar, Apr, May, Jun, Jul, Aug, Sept, Oct, Nov, Dec") {
            mthsSelected = "All months";
        } else {
            mthsSelected = mthsSelected;
        }
    } else {
        /* if no days where selected, sets the default to 'All months' */ 
        mthsSelected = "All months";
    }

    selectedMths = mthsSelected;
}

$('#activeButton, #addAnotherRuleButton, #addMoreRulesButton, #modalCloseButton').click(function () {
    if (this.id != 'activeButton') {
        location.reload();
    }

    $('#activeButton').addClass('btn-primary');
    $('#inactiveButton').removeClass('btn-primary');
    $('#inactiveButton').addClass('btn-default');

    active();
    pageState = 'Active';
});

// Active button function to set HTML attributes
var active = function () {
    $("#activeInactiveHeader").html('Active');
    $("button[name = 'modifyButton'], button[name = 'deleteButton']").css("display", "block");
    $("#orderPositionRow, #callRangeRow").css("display", "block");

    defaults();
}

var defaults = function () {
    $("#todrexitingrules, #ruleBuilder, #initialAddTip, #dateErrorMessage").css("display", "none");
}

enter image description here enter image description here

2 个答案:

答案 0 :(得分:1)

您已将“ selectedWkDays”设置为作为数组的全局变量。第一次运行“ getSelectedWeekdayValues”函数时,它将在该函数末尾(如上面的注释中所述)将selectedWkDays变量重置为字符串形式的“ wkDaysSelected”的值。

第二次运行时,它将尝试“推”和“联接”到没有这些方法的字符串。

答案 1 :(得分:1)

完成Jarrod的答案。

您可以返回值,而不是将字符串设置为数组。您可以将数组声明为函数。

function getSelectedWeekdayValues(){  
    var selectedWkDays = [ ];
    [...]
    return wkDaysSelected;
}

在点击事件中,您可以使用返回的值设置全局变量

var selectedWeekdayValues;
$('#submitButton').click(function () {
    selectedWeekdayValues = getSelectedWeekdayValues();