简体   繁体   English

从ejs检索JSON值

[英]Retrieve JSON values from ejs

I am new to node and ejs, I have below code, and need to know how to get the values from EJs file to post it to mongoDB 我是node和ejs的新手,我有下面的代码,并且需要知道如何从EJs文件中获取值并将其发布到mongoDB

EJS file EJS文件

<form>
    <p>
        <label for="username"> Username </label>
        <input type="text" for="username"/>
    </p>
    <p>
        <label for="password"> Password </label>
        <input type="password" for="password" />
    </p>
    <p>
        <label for="email"> Email </label>
        <input type="text" for="email"/>
    </p>
    <button type="submit">Log In</button> 
</form>

JS file JS文件

$('form').on('submit', function(){

      var item = $('form input');
      var user = [{username:item.val()},{password:item.val()},{email:item.val()}];

      console.log(user);

      $.ajax({
        type: 'POST',
        url: '/register',
        data: user,
        success: function(data){
          //do something with the data via front-end framework
            console.log(data);
          location.reload();
        }
      });

      return false;

  });

As of now when I insert to mongo DB I get a new uuid but the values are going in as "0" 截至目前,当我插入到mongo DB中时,我得到了一个新的uuid,但其值将变为"0"

please aasist 请客气

So the line var item = $('form input'); 因此,行var item = $('form input'); is actually a collection of each input, while the line var user = [{username:item.val()},{password:item.val()},{email:item.val()}]; 实际上是每个输入的集合,而var user = [{username:item.val()},{password:item.val()},{email:item.val()}]; is only grabbing the value of the first one (this is because of how jQuery can appear to hide a collection), instead I would try this (assuming you wanted to grab specific values out of that form): 只是获取第一个的值(这是因为jQuery似乎可以隐藏一个集合),而我会尝试这样做(假设您想从该表单中获取特定的值):

var items = $('form');
var user = {
      username: $('[name=username]', items).val(),
      password: $('[name=password]', items).val(),
      email: $('[name=email]', items).val()
    };

With following change to your html 随着以下更改您的HTML

<form>
    <p>
        <label for="input-username"> Username </label>
        <input type="text" id="input-username" name="username"/>
    </p>
    <p>
        <label for="input-password"> Password </label>
        <input type="password" id="input-password" name="password" />
    </p>
    <p>
        <label for="input-email"> Email </label>
        <input type="text" id="input-email" name="email"/>
    </p>
    <button type="submit">Log In</button> 
</form> 

Also the for attribute in the <label> element is to associate with an <input> that has an id attribute with the same value. 同样, <label>元素中的for属性也要与具有相同id属性的<input>关联。 So considering that id s should be unique and only resolve to a single element I would suggest adding some kind of name spacing like input- to them. 因此,考虑到id应该是唯一的,并且只能解析为单个元素,我建议为它们添加某种名称间隔,例如input-

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM