英文:
Can't append options in select tag
问题
这是你提供的代码的翻译:
"Can someone please tell what I am doing wrong?"
能否有人告诉我我做错了什么?
var $whatSummary = $("<p></p>");
var remindersContent2 = {
"Variant A": {
"day": [
-1,
0
],
"ids": [
"D-1",
"D0"
],
"qm": {
"a": b
},
"msg": {
"a": b
}
},
"Variant B": {
"day": [
1,
2
],
"ids": [
"D1",
"D2"
],
"qm": {
"x": y
},
"msg": {
"x": y
}
}
}
$whatSummary.append("<select class='js-variantSelectContent'></select>")
for (var key in remindersContent2) {
var pkName =_dashUtils.getVariantName(remindersContent2[key]['day'])
$('.js-variantSelectContent').append($("<option value ='" + key + "'>" + pkName + "</option>"))
}
请注意,我已经保留了原始代码中的HTML和JavaScript部分,只对文本进行了翻译。如果您需要进一步的帮助或解释,请告诉我。
英文:
Can someone please tell what I am doing wrong ?
var $whatSummary = $("<p></p>");
var remindersContent2 = {
"Variant A": {
"day": [
-1,
0
],
"ids": [
"D-1",
"D0"
],
"qm": {
"a": b
},
"msg": {
"a": b
}
},
"Variant B": {
"day": [
1,
2
],
"ids": [
"D1",
"D2"
],
"qm": {
"x": y
},
"msg": {
"x": y
}
}
}
$whatSummary.append("<select class='js-variantSelectContent'></select>")
for (var key in remindersContent2) {
var pkName =_dashUtils.getVariantName(remindersContent2[key]['day'])
$('.js-variantSelectContent').append($("<option value ='" + key + "'>" + pkName + "</option>"))
}`
By writing above code I want to add a variant selector which will display the data as per the variant pkName. Here $whatSummary is an empty container in which we are appending a selector as per our need.
Here pkName is getting populated properly with a string value like "-1st day to due date" for Variant A and "+1st day to +2nd day" for Variant B respectively. But the options are not getting appended with the pkNames, can someone help?
答案1
得分: 1
在将$whatSummary
附加到DOM之前,选择器.js-variantSelectContent
无法找到其内部的选择元素。
您可以先将选择元素存储在单独的变量中。
const $select = $("<select class='js-variantSelectContent'/>");
$whatSummary.append($select);
for (const key in remindersContent2) {
// ...
$select.append($("<option value ='" + key + "'>" + pkName + "</option>"))
}
// 在之后将$whatSummary添加到DOM中...
英文:
Before $whatSummary
is appended to the DOM, the selector .js-variantSelectContent
cannot find the select element inside it.
You can store the select element in a separate variable first.
const $select = $("<select class='js-variantSelectContent'/>");
$whatSummary.append($select);
for (const key in remindersContent2) {
// ...
$select.append($("<option value ='" + key + "'>" + pkName + "</option>"))
}
// add $whatSummary to the DOM after...
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论