使用nodejs将数据从表单输入到mongodb

时间:2018-10-11 20:02:05

标签: javascript node.js mongodb express mongoose

根据从表单中选择的选项,我无法在数据库中输入数据 这是我的表格

    <div class="input">
        <label for="fever">Fever</label>
        <input type="radio" id="fever" name="fever" value="0">No
        <input type="radio" id="fever" name="fever" value="1">Yes<br><br>
    </div>
    <div class="input">
        <label for="eyesight">Eyesight</label>
        <input type="radio" id="eyesight" name="eyesight" value="0">Long Sightedness
        <input type="radio" id="eyesight" name="eyesight" value="1">Short Sightedness<br><br>
    </div>

我怎么知道用户选择了哪个选项并将其输入我的数据库

2 个答案:

答案 0 :(得分:1)

可能您需要执行以下操作才能获得所选值jQuery get value of selected radio button

答案 1 :(得分:0)

首先,您不需要为输入使用重复的ID。然后,您可以使用.checked

找出选择了哪个选项

例如:

const el = document.getElementById('clickMe');
    
    el.onclick = function() {
    	if (document.getElementById('fever0').checked) {
        alert('No fever');
      }
      if (document.getElementById('fever1').checked) {
        alert('Yes fever');
      }
    	if (document.getElementById('eyesight0').checked) {
        alert('Long Sightedness');
      }
      if (document.getElementById('eyesight1').checked) {
        alert('Short Sightedness');
      }
    };
<div class="input">
      <label for="fever">Fever</label>
      <input type="radio" id="fever0" name="fever" value="0">No
      <input type="radio" id="fever1" name="fever" value="1">Yes<br><br>
    </div>
    <div class="input">
      <label for="eyesight">Eyesight</label>
      <input type="radio" id="eyesight0" name="eyesight" value="0">Long Sightedness
      <input type="radio" id="eyesight1" name="eyesight" value="1">Short Sightedness<br><br>
    </div>
    <div id="clickMe">
      Click me!
    </div>