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

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

Can't append options in select tag

问题

这是你提供的代码的翻译:

"Can someone please tell what I am doing wrong?"

  1. 能否有人告诉我我做错了什么
  2. var $whatSummary = $("<p></p>");
  3. var remindersContent2 = {
  4. "Variant A": {
  5. "day": [
  6. -1,
  7. 0
  8. ],
  9. "ids": [
  10. "D-1",
  11. "D0"
  12. ],
  13. "qm": {
  14. "a": b
  15. },
  16. "msg": {
  17. "a": b
  18. }
  19. },
  20. "Variant B": {
  21. "day": [
  22. 1,
  23. 2
  24. ],
  25. "ids": [
  26. "D1",
  27. "D2"
  28. ],
  29. "qm": {
  30. "x": y
  31. },
  32. "msg": {
  33. "x": y
  34. }
  35. }
  36. }
  37. $whatSummary.append("<select class='js-variantSelectContent'></select>")
  38. for (var key in remindersContent2) {
  39. var pkName =_dashUtils.getVariantName(remindersContent2[key]['day'])
  40. $('.js-variantSelectContent').append($("<option value ='" + key + "'>" + pkName + "</option>"))
  41. }

请注意,我已经保留了原始代码中的HTML和JavaScript部分,只对文本进行了翻译。如果您需要进一步的帮助或解释,请告诉我。

英文:

Can someone please tell what I am doing wrong ?

  1. var $whatSummary = $(&quot;&lt;p&gt;&lt;/p&gt;&quot;);
  2. var remindersContent2 = {
  3. &quot;Variant A&quot;: {
  4. &quot;day&quot;: [
  5. -1,
  6. 0
  7. ],
  8. &quot;ids&quot;: [
  9. &quot;D-1&quot;,
  10. &quot;D0&quot;
  11. ],
  12. &quot;qm&quot;: {
  13. &quot;a&quot;: b
  14. },
  15. &quot;msg&quot;: {
  16. &quot;a&quot;: b
  17. }
  18. },
  19. &quot;Variant B&quot;: {
  20. &quot;day&quot;: [
  21. 1,
  22. 2
  23. ],
  24. &quot;ids&quot;: [
  25. &quot;D1&quot;,
  26. &quot;D2&quot;
  27. ],
  28. &quot;qm&quot;: {
  29. &quot;x&quot;: y
  30. },
  31. &quot;msg&quot;: {
  32. &quot;x&quot;: y
  33. }
  34. }

}

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

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无法找到其内部的选择元素。

您可以先将选择元素存储在单独的变量中。

  1. const $select = $(&quot;&lt;select class=&#39;js-variantSelectContent&#39;/&gt;&quot;);
  2. $whatSummary.append($select);
  3. for (const key in remindersContent2) {
  4. // ...
  5. $select.append($(&quot;&lt;option value =&#39;&quot; + key + &quot;&#39;&gt;&quot; + pkName + &quot;&lt;/option&gt;&quot;))
  6. }
  7. // 在之后将$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.

  1. const $select = $(&quot;&lt;select class=&#39;js-variantSelectContent&#39;/&gt;&quot;);
  2. $whatSummary.append($select);
  3. for (const key in remindersContent2) {
  4. // ...
  5. $select.append($(&quot;&lt;option value =&#39;&quot; + key + &quot;&#39;&gt;&quot; + pkName + &quot;&lt;/option&gt;&quot;))
  6. }
  7. // 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:

确定