Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/79.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Jquery SweetAlert下拉列表动态添加列表中的项目_Jquery_Json_Sweetalert - Fatal编程技术网

Jquery SweetAlert下拉列表动态添加列表中的项目

Jquery SweetAlert下拉列表动态添加列表中的项目,jquery,json,sweetalert,Jquery,Json,Sweetalert,我目前正在使用sweetalert2从对话框中捕获用户的输入。我想使用“链接队列”对话框中的下拉列表,但似乎找不到在下拉列表中动态添加项目的方法。假设我想从JSON格式中检索数据并将其放在下拉列表中,有什么方法可以做到这一点吗 function userInput() { swal.setDefaults( { showLoaderOnConfirm: true, confirmButtonText: 'Next &

我目前正在使用sweetalert2从对话框中捕获用户的输入。我想使用“链接队列”对话框中的下拉列表,但似乎找不到在下拉列表中动态添加项目的方法。假设我想从JSON格式中检索数据并将其放在下拉列表中,有什么方法可以做到这一点吗

function userInput() {
    swal.setDefaults(
        {
            showLoaderOnConfirm: true,
            confirmButtonText: 'Next →',
            showCancelButton: true,
            animation: true,
            progressSteps: ['1', '2', '3']
        }
    );

    var steps = [
        {
            text: 'Select an author to analyze the commit',
            input: 'select',
            inputOptions: {
                'SRB': 'Serbia',     // How do I dynamically set value?
                'UKR': 'Ukraine',
                'HRV': 'Croatia'
            }
        },
        {
            text: 'Select multiple authors to compare their commits',
            input: 'select',
            inputOptions: {
                'SRB': 'Serbia',      // How do I dynamically set value?
                'UKR': 'Ukraine',
                'HRV': 'Croatia'
            }
        },
        {
            text: 'Select a file directory to analyze all author\'s commit',
            input: 'select',
            inputOptions: {
                'SRB': 'Serbia',      // How do I dynamically set value?
                'UKR': 'Ukraine',
                'HRV': 'Croatia'
            }
        }
    ];

    swal.queue(steps).then(function(result) {
        swal.resetDefaults();
        swal({
            type: 'success',
            title: 'Success',
            text: 'Scroll down for statistics!',
            html:
                'Your selections: <pre>' +
                JSON.stringify(result) +
                '</pre>',
            confirmButtonText: 'Ok',
            showCancelButton: false
        })
    }, function() {
        swal.resetDefaults()
    })
}
如上所述,
inputOptions
参数可以是
object
Promise

要动态填充选择项,您应该使用承诺,下面是一个简单的示例:

var-inputoptionpromise=新承诺(函数(解析){
//获取数据并将其传递给resolve()
setTimeout(函数(){
决心({
“#FF0000”:“红色”,
“#00FF00”:“绿色”,
“#0000FF”:“蓝色”
})
}, 2000)
})
喷火({
输入:“选择”,
输入选项:输入选项承诺
})

如何在解决中添加更多选项时使用$??
function getData(repolink) {
 readDataFromGit('https://api.github.com/repos/' + repolink + '/git/trees/master?recursive=1', function(text){
        data = JSON.parse(text);
        $.each(data, function(i, v) {
            // Retrieve v.login data!
            data = v.login;
        })
    });
}

function readDataFromGit(link, callback) {
    var request = new XMLHttpRequest();
    request.overrideMimeType("application/json");
    request.open('GET', link, true);
    request.onreadystatechange = function() {
        if (request.readyState === 4 && request.status == "200") {
            callback(request.responseText);
        }
    };
    request.send(null);
}