在 Post 请求中获取单选按钮的值

真实的

我正在尝试编写一个程序,该程序需要两条信息,一个名称和一个单选按钮值。我似乎无法在发布请求中获取单选按钮的实际值。即使我选中值为 1 的单选按钮,ejs 页面也会一直显示“无符号”和控制台日志为未定义。任何帮助都会很棒。

theApp.post('/sendUserData', function(req, res) {

    var name = req.body.first;

    var radioButtonVal = req.query.imagePick;

    console.log(name);
    console.log(radioButtonVal);

    res.render('template.ejs', {theName: name, theSign: radioButtonVal});
});

<body>
<h1>Hi <%= theName %> </h1>

<h1> sign:
<% if (theSign == "1") { %>
        Aries
    <% } else if (theSign == "2") { %>
        Taurus
    <% } else { %>
        No Sign
    <% } %>
    </h1>
</body>
<h2 id="title">astrology reading</h2><br/>

    <form name="Input" method="POST" action="/sendUserData">
          your First Name: <input name="first" type="text">
      <br/><br/><br/>
      your star sign: <br/>    
      <div class="container">
            <input name="imagePick" value="1" type="radio" id="one">
            <label for="one">
                <img src="images/aries.png" alt="aries" height="150" width="auto">
            </label>

            <input name="imagePick" value="2" type="radio" id="two">
            <label for="two">
                <img src="images/taurus.png" alt="taurus" height="150" width="auto">
            </label>
        </div>

            <input type="submit" name="submitButton" value="Save">

    </form>
10ZKhaled

这是因为您应该从正文而不是从查询中请求 radioButtonVal。试试下面的代码片段,它应该可以工作:

theApp.post('/sendUserData', function(req, res) {

    var name = req.body.first;

    var radioButtonVal = req.body.imagePick;

    console.log(name);
    console.log(radioButtonVal);

    res.render('template.ejs', {theName: name, theSign: radioButtonVal});
});

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章