这些单选按钮中的训练逗号来自哪里?

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

Where are the training commas coming from in these radio butons?

问题

由于某些原因,选项的末尾有一个尾随逗号。这不在模板中,我不确定它是从哪里来的。

起初,我以为是某个脚本的问题,但即使在隔离后,问题似乎仍然存在。

const options = [
  "Easy to use",
  "Loading",
  "Fast",
  "Others (Mention below)"
];
const template = `
<div class="question-title">sdjhaskjdhakjs</div>
<div class="question-options">${options?.map((option, i) => {
  return `
  <div class="question-option">
    <input class="${"id"}-feedback-question-option" type="radio" id="${"id"}-${option}" name="option" value="${option}" ${
    options.length === i - 1 ? "checked" : ""
  } />
    <label for="${"id"}-${option}">${option}</label>
  </div>`;
})}</div>
`;

document.getElementById("app").innerHTML = template;
.question-title {
  position: relative;
  height: 40px;      
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: normal;
  font-size: 16px;
}

.question-options {
  position: relative;
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: normal;
  font-size: 12px;
  gap: 10px;
  flex-warp: warp;
}

.question-option {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 5px;
}

.question-option>label {
  margin-bottom: unset;
}

input[type=radio]::after {
  content: '';
}
<!DOCTYPE html>
<html>
  <head>
    <title>Parcel Sandbox</title>
    <meta charset="UTF-8" />
  </head>

  <body>
    <div id="app"></div>
  </body>
</html>
英文:

For some reasons end of the options has a trailing comma. It's not in the template, I am not sure where its coming from.

At first I thought it was some script, but even after isolation the issue seems to persists.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

const options = [
        &quot;Easy to use&quot;,
        &quot;Loading&quot;,
        &quot;Fast&quot;,
        &quot;Others (Mention below)&quot;
      ];
      const template = `
    &lt;div class=&quot;question-title&quot;&gt;sdjhaskjdhakjs&lt;/div&gt;
    &lt;div class=&quot;question-options&quot;&gt;${options?.map((option, i) =&gt; {
      return `
      &lt;div class=&quot;question-option&quot;&gt;
        &lt;input class=&quot;${&quot;id&quot;}-feedback-question-option&quot; type=&quot;radio&quot; id=&quot;${&quot;id&quot;}-${option}&quot; name=&quot;option&quot; value=&quot;${option}&quot; ${
        options.length === i - 1 ? &quot;checked&quot; : &quot;&quot;
      } /&gt;
        &lt;label for=&quot;${&quot;id&quot;}-${option}&quot;&gt;${option}&lt;/label&gt;
      &lt;/div&gt;`;
    })}&lt;/div&gt;
  `;
 
 document.getElementById(&quot;app&quot;).innerHTML = template;

<!-- language: lang-css -->

.question-title {
      position: relative;
      height: 40px;      
      display: flex;
      align-items: center;
      justify-content: center;
      font-weight: normal;
      font-size: 16px;
    }

    .question-options {
      position: relative;
      display: flex;
      align-items: center;
      justify-content: center;
      font-weight: normal;
      font-size: 12px;
      gap: 10px;
      flex-warp: warp;
    }

    .question-option {
      display: flex;
      align-items: center;
      justify-content: center;
      gap: 5px;
    }

    .question-option&gt;label {
      margin-bottom: unset;
    }

    input[type=radio]::after {
      content: &#39;&#39;;
    }

<!-- language: lang-html -->

&lt;!DOCTYPE html&gt;
&lt;html&gt;
  &lt;head&gt;
    &lt;title&gt;Parcel Sandbox&lt;/title&gt;
    &lt;meta charset=&quot;UTF-8&quot; /&gt;
  &lt;/head&gt;

  &lt;body&gt;
    &lt;div id=&quot;app&quot;&gt;&lt;/div&gt;
  &lt;/body&gt;
&lt;/html&gt;

<!-- end snippet -->

答案1

得分: 5

Arrays converted to string are automatically joined with ,

Use .join('') to bypass this behavior

英文:

Arrays converted to string are automatically joined with ,

Use .join(&#39;&#39;) to bypass this behaviour

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

const options = [
  &quot;Easy to use&quot;,
  &quot;Loading&quot;,
  &quot;Fast&quot;,
  &quot;Others (Mention below)&quot;
];
const template = `
    &lt;div class=&quot;question-title&quot;&gt;sdjhaskjdhakjs&lt;/div&gt;
    &lt;div class=&quot;question-options&quot;&gt;${options?.map((option, i) =&gt; {
      return `
      &lt;div class=&quot;question-option&quot;&gt;
        &lt;input class=&quot;${&quot;id&quot;}-feedback-question-option&quot; type=&quot;radio&quot; id=&quot;${&quot;id&quot;}-${option}&quot; name=&quot;option&quot; value=&quot;${option}&quot; ${
        options.length === i - 1 ? &quot;checked&quot; : &quot;&quot;
      } /&gt;
        &lt;label for=&quot;${&quot;id&quot;}-${option}&quot;&gt;${option}&lt;/label&gt;
      &lt;/div&gt;`;
    }).join(&#39;&#39;)}&lt;/div&gt;
  `;

document.getElementById(&quot;app&quot;).innerHTML = template;

<!-- language: lang-css -->

.question-title {
  position: relative;
  height: 40px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: normal;
  font-size: 16px;
}

.question-options {
  position: relative;
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: normal;
  font-size: 12px;
  gap: 10px;
  flex-warp: warp;
}

.question-option {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 5px;
}

.question-option&gt;label {
  margin-bottom: unset;
}

input[type=radio]::after {
  content: &#39;&#39;;
}

<!-- language: lang-html -->

&lt;!DOCTYPE html&gt;
&lt;html&gt;
  &lt;head&gt;
    &lt;title&gt;Parcel Sandbox&lt;/title&gt;
    &lt;meta charset=&quot;UTF-8&quot; /&gt;
  &lt;/head&gt;

  &lt;body&gt;
    &lt;div id=&quot;app&quot;&gt;&lt;/div&gt;
  &lt;/body&gt;
&lt;/html&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年6月26日 15:02:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/76554231.html
匿名

发表评论

匿名网友

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

确定