英文:
How can I map the object key and value in option HTML tags in appscript
问题
我从Code.gs中的一个函数中检索到了以下对象。我想将键值对传递到我的HTML下拉按钮中。
如何将对象的键和值映射到选项HTML标签中?
HTML
<div class="form-group">
<? var data = listAccounts() ?>
<optgroup label="Ad Accounts">
<? for (i=0; i<data.length; i++) { ?>
<option value="<?= data[i] ?>"><?= data[i] ?></option>
<? } ?>
</optgroup>
</div>
从函数listAccounts()返回的示例对象
{
'abc': 'accountSummaries/123',
'def': 'accountSummaries/124',
'ghi': 'accountSummaries/125',
'jkl Accounts': 'accountSummaries/1405'
}
英文:
I am retrieving this below object from a function in Code. gs. I would like to pass the key-value pair into my HTML drop-down button.
How can I map the object key and value in option HTML tags?
HTML
<div class="form-group">
<? var data = listAccounts() ?>
<optgroup label="Ad Accounts">
<? for (i=0; i<data.length; i++) { ?>
<option value="<?= data[i] ?>"> <?= data[i] ?>) </option>
</optgroup>
</div>
Example Object Returned from a function listAccounts()
{
'abc': 'accountSummaries/123',
'def': 'accountSummaries/124',
'ghi': 'accountSummaries/125',
'jkl Accounts': 'accountSummaries/1405'
}
答案1
得分: 2
以下是翻译好的部分:
嗯,这是Javascript部分,所以我猜...这应该可以完成任务?
https://jsfiddle.net/MarinHTML/z29o5kaj/11/
var abc = { 'abc': 'accountSummaries/123',
'def': 'accountSummaries/124',
'ghi': 'accountSummaries/125',
'jkl Accounts': 'accountSummaries/1405'
};
for(let i=0;i<Object.keys(abc).length;i++) {
$('#gg').append('<option data-key="'+Object.keys(abc)[i]+'" data-value="'+Object.values(abc)[i]+'">'+Object.values(abc)[i]+'</option>')
}
^ 使用jQuery的示例映射。或者你想用PHP来做吗?这也可以工作,但你必须指出你想要键和值的位置。
英文:
Hmm, it's the Javascript section, so I guess... this should do the job?
https://jsfiddle.net/MarinHTML/z29o5kaj/11/
var abc = { 'abc': 'accountSummaries/123',
'def': 'accountSummaries/124',
'ghi': 'accountSummaries/125',
'jkl Accounts': 'accountSummaries/1405'
};
for(let i=0;i<Object.keys(abc).length;i++) {
$('#gg').append('<option data-key="'+Object.keys(abc)[i]+'" data-value="'+Object.values(abc)[i]+'">'+Object.values(abc)[i]+'</option>')
}
^Example mapping using jQuery. Or do you want to do it with PHP? This can also work, but you must point out where you want the key and where you want the value.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论