无法在选择标签中添加选项。

huangapple go评论63阅读模式
英文:

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 = $(&quot;&lt;p&gt;&lt;/p&gt;&quot;);

var remindersContent2 = {
&quot;Variant A&quot;: {
    &quot;day&quot;: [
        -1,
        0
    ],
    &quot;ids&quot;: [
        &quot;D-1&quot;,
        &quot;D0&quot;
    ],
    &quot;qm&quot;: {
        &quot;a&quot;: b
    },
    &quot;msg&quot;: {
        &quot;a&quot;: b
    }
},
&quot;Variant B&quot;: {
    &quot;day&quot;: [
        1,
        2
    ],
    &quot;ids&quot;: [
        &quot;D1&quot;,
        &quot;D2&quot;
    ],
    &quot;qm&quot;: {
        &quot;x&quot;: y
    },
    &quot;msg&quot;: {
        &quot;x&quot;: y
    }
}

}

    $whatSummary.append(&quot;&lt;select class=&#39;js-variantSelectContent&#39;&gt;&lt;/select&gt;&quot;)
    for (var key in remindersContent2) {
      var pkName =_dashUtils.getVariantName(remindersContent2[key][&#39;day&#39;])
      $(&#39;.js-variantSelectContent&#39;).append($(&quot;&lt;option value =&#39;&quot; + key + &quot;&#39;&gt;&quot; + pkName + &quot;&lt;/option&gt;&quot;))
    }`

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 = $(&quot;&lt;select class=&#39;js-variantSelectContent&#39;/&gt;&quot;);
$whatSummary.append($select);
for (const key in remindersContent2) {
	// ...
	$select.append($(&quot;&lt;option value =&#39;&quot; + key + &quot;&#39;&gt;&quot; + pkName + &quot;&lt;/option&gt;&quot;))
}
// 在之后将$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 = $(&quot;&lt;select class=&#39;js-variantSelectContent&#39;/&gt;&quot;);
$whatSummary.append($select);
for (const key in remindersContent2) {
	// ...
	$select.append($(&quot;&lt;option value =&#39;&quot; + key + &quot;&#39;&gt;&quot; + pkName + &quot;&lt;/option&gt;&quot;))
}
// add $whatSummary to the DOM after...

huangapple
  • 本文由 发表于 2023年6月5日 03:16:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/76402056.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定