Create button array for jquery dialog

吃可爱长大的小学妹 提交于 2020-01-05 08:38:29

问题


Is it possible to create a button array then append it to a jQuery dialog?

Something along these lines. Forgive my errorful code within the for loop, just not sure how to do this at all.

function setAutoDialog()
{
    var testArray = ["T1", "T2"];
    $('#autoDialog').dialog({
        autoOpen: false,
        width: 'auto',
    });
    var buttons = {};
    for(var i=0; i<testArray.length; i++){
        buttons += [testArray[ix] : Test()]
    }
    $('#autoDialog').dialog('option', 'buttons', buttons);
}

function Test()
{
    alert("worked");
}

回答1:


Something like this should work :

function setAutoDialog(){
    var testArray = ["T1", "T2"];

    var testFunction = function () {
        alert("worked");
    }

    var myButtons = {};

    for(var i = 0; i < testArray.length; i++){
        myButtons[testArray[i]] = testFunction;
    }

    $('#autoDialog').dialog({
        autoOpen: false,
        width: 'auto',
        buttons : myButtons
    }); 
}

"For instance on click instead of alert(worked) I want to get alert(buttonClicked.val())?" It would be something like :

function setAutoDialog(){
    var testArray = ["T1", "T2"];

    var myButtons = {};

    for(var i = 0; i < testArray.length; i++){
        var testFunction = function () {
            alert(testArray[i]);
        }

        myButtons[testArray[i]] = testFunction;
    }

    $('#autoDialog').dialog({
        autoOpen: false,
        width: 'auto',
        buttons : myButtons
    }); 
}


来源:https://stackoverflow.com/questions/14389735/create-button-array-for-jquery-dialog

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!