单选框输入将“on”作为数据库值存储。

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

radio box input stores "on" as database value

问题

单选框输入不会存储值,而是在数据库中显示 "on"。以下是我的代码:

<label for="gender">Female</label>
<input type="radio" name="gender">
<label for="gender">Male</label>
<input type="radio" name="gender">
<label for="gender">Transgender</label>
<input type="radio" name="gender">
<span class="formerror"></span>

这是我的模式:

gender: {
  type: String,
  required: true
}

这是数据库中存储的数据:

{
  name: 'Ethan Hunt',
  age: 60,
  gender: 'on',
  blood_group: 'B+',
  date: 2023-07-05T00:00:00.000Z,
  phone_number: '7074788341',
  medical_history: 'cataract',
  password: '1234',
  _id: new ObjectId("64a1bced4796f50508c67030"),
  __v: 0
}

我期望性别值显示为 "male"、"female" 或 "transgender"。

我还尝试添加以下内容:

gender: {
  type: String,
  enum: ["female", "male", "transgender"],
  required: true
}

但它生成了错误消息:"详细验证失败:gender:'on' 不是路径 'gender' 的有效枚举值。在 ValidationError 中"。

英文:

radio box input doesn't store the value instead shows "on" in the database
here is my code.

            &lt;label for=&quot;gender&quot;&gt;Female&lt;/label&gt;
            &lt;input type=&quot;radio&quot; name=&quot;gender&quot;&gt;
            &lt;label for=&quot;gender&quot;&gt;Male&lt;/label&gt;
            &lt;input type=&quot;radio&quot; name=&quot;gender&quot;&gt;
            &lt;label for=&quot;gender&quot;&gt;Transgender&lt;/label&gt;
            &lt;input type=&quot;radio&quot; name=&quot;gender&quot;&gt;
            &lt;span class=&quot;formerror&quot;&gt;&lt;/span&gt;

this is my schema

gender: {
        type: String,
        required: true
      }

this is the data in the database stored

{
  name: &#39;Ethan Hunt&#39;,
  age: 60,
  gender: &#39;on&#39;,
  blood_group: &#39;B+&#39;,
  date: 2023-07-05T00:00:00.000Z,
  phone_number: &#39;7074788341&#39;,
  medical_history: &#39;cataract&#39;,
  password: &#39;1234&#39;,
  _id: new ObjectId(&quot;64a1bced4796f50508c67030&quot;),
  __v: 0
}

i was expecting the gender value to show up as "male" "female" or "transgender".

i also tried adding

gender: {
        type: String,
        enum: [&quot;female&quot;, &quot;male&quot;, &quot;transgender&quot;],
        required: true
      },

but it generates an error
"Detail validation failed: gender: on is not a valid enum value for path gender.
at ValidationError"

答案1

得分: 1

为您的HTML元素添加一个 "value" 属性,并将其值设置为表示性别的值。

还可以阅读:https://developer.mozilla.org/en-US/docs/Web/API/Element/getAttribute 以获取有关通过JavaScript访问该值的更多信息。

英文:

give your html element a "value" attribute and set it's value to the gender value it denotes.

also read: https://developer.mozilla.org/en-US/docs/Web/API/Element/getAttribute
for more insight on accessing the value via javaScript

答案2

得分: 1

你需要为每个选项/单选按钮设置一个值。现在你可以通过名称(gender)来获取值。在提交事件的回调中,e.target是表单元素。

for 属性在 <label> 中只对ID有效。跳过ID,将输入作为标签的子元素将正常工作。

<form name="form01">
  <label>Female <input type="radio" name="gender" value="female" required></label>
  <label>Male <input type="radio" name="gender" value="male"></label>
  <label>Transgender <input type="radio" name="gender" value="transgender"></label>
  <button type="submit">Submit</button>
</form>
document.forms.form01.addEventListener('submit', e => {
  e.preventDefault();
  console.log(e.target.gender.value);
});
英文:

You need to give each option/radio button a value. And now you can ask for the value on the name (gender). The e.target in the submit event callback is the form element.

The for attribute on &lt;label&gt; only work for IDs. Skip the IDs and have the input as a child to label will work fine.

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

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

document.forms.form01.addEventListener(&#39;submit&#39;, e =&gt; {
  e.preventDefault();
  console.log(e.target.gender.value);
});

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

&lt;form name=&quot;form01&quot;&gt;
  &lt;label&gt;Female &lt;input type=&quot;radio&quot; name=&quot;gender&quot; value=&quot;female&quot; required&gt;&lt;/label&gt;
  &lt;label&gt;Male &lt;input type=&quot;radio&quot; name=&quot;gender&quot; value=&quot;male&quot;&gt;&lt;/label&gt;
  &lt;label&gt;Transgender &lt;input type=&quot;radio&quot; name=&quot;gender&quot; value=&quot;transgender&quot;&gt;&lt;/label&gt;
  &lt;button type=&quot;submit&quot;&gt;Submit&lt;/button&gt;
&lt;/form&gt;

<!-- end snippet -->

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

发表评论

匿名网友

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

确定