gpt4 book ai didi

Javascript - Sweetalert - 添加值到选择列表

转载 作者:行者123 更新时间:2023-11-30 15:53:37 26 4
gpt4 key购买 nike

我正在使用 SweetAlert2,并且有一个选择列表。我的挑战是选择列表中的值是以编程方式添加的。当我的代码运行时,下拉列表具有正确的 NUMBER 个值,文本显示 [object Object] 而不是我添加的内容。我究竟做错了什么?代码如下。

     var outputStr = [];     

for (var i = 0; i < data.rows.length; i++) {

// If here, we have data, so show the information....
var vREGISTRY_ID = data.rows[i].REGISTRY_ID ? data.rows[i].REGISTRY_ID : '-';
var vNN_NAME = data.rows[i].NN_NAME ? data.rows[i].NN_NAME : '-';
var vACCOUNT_NAME = data.rows[i].ACCOUNT_NAME ? data.rows[i].ACCOUNT_NAME : '-';
var vSITE_DUNS_9DIG = data.rows[i].SITE_DUNS_9DIG ? data.rows[i].SITE_DUNS_9DIG : '-';
var vPRIMARY_CITY = data.rows[i].PRIMARY_CITY ? data.rows[i].PRIMARY_CITY : '-';
var vPRIMARY_STATE_PROVINCE = data.rows[i].PRIMARY_STATE_PROVINCE ? data.rows[i].PRIMARY_STATE_PROVINCE : '-';

outputStr.push({
value:vREGISTRY_ID,
label: vACCOUNT_NAME
}) ;


}; // end of FOR loop

swal({
title: 'Select Account Name or Division',
input: 'select',
inputOptions: outputStr ,
inputPlaceholder: 'Select from dropdown',
showCancelButton: true,
inputValidator: function(value) {
return new Promise(function(resolve, reject) {
if (value === 'abc') {
resolve();
} else {
reject('You need to select abc :)');
}
});
}
}).then(function(result) {
swal({
type: 'success',
html: 'You selected: ' + result
});
})

最佳答案

您必须向 JavaScript 对象添加动态属性

像这样:data[propertyName] = propertyValue;

    var inputOptions = {}; // Define like this!

// Instead of sample variables,
// your data handling here

var vREGISTRY_ID = "500";
var vACCOUNT_NAME = "Peter";



// Add the Variables like this
// This will create '500' : 'Peter',
inputOptions[vREGISTRY_ID] = vACCOUNT_NAME;
inputOptions["455"] = "Martin";

// Note that the options will get sorted by their value

swal({
title: 'Select Account Name or Division',
input: 'select',
inputOptions: inputOptions,
inputPlaceholder: 'Select from dropdown',
showCancelButton: true,
inputValidator: function(value) {
return new Promise(function(resolve, reject) {
if (value == "500") {
resolve();
} else {
reject('You need to select Peter :)');
}
});
}
}).then(function(result) {
swal({
type: 'success',
html: 'You selected: ' + result
});
})
<link href="https://cdn.jsdelivr.net/sweetalert2/4.1.5/sweetalert2.css" rel="stylesheet"/>
<script src="https://cdn.jsdelivr.net/sweetalert2/4.1.5/sweetalert2.js"></script>

关于Javascript - Sweetalert - 添加值到选择列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38923796/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com