最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Get values of checked radio button with post request in Node.js - Stack Overflow

programmeradmin3浏览0评论

I have a few radio buttons a user can check to add search parameters to their search query. When I make the post request to my server, I am getting 'on' as the value.

Here's a snippet of the HTML:

<form method="post" action="/">
  <input type="radio" name="sushi">
  <label for="sushi">Sushi</label>
  <button type="submit">Enter</button>
</form>

server.js:

app.post('/', (req, res) => {
     const request = req.body;
     let sushi = request.sushi;

    console.log(sushi); //returns 'on'
});

I can get the value from a text input field using this method with no problem, but the radio buttons do not work this way. They only return a value of on if the radios are clicked on submit. I know I am missing something, but I'm having trouble finding any solutions that don't relate to the frontend and JQuery.

How can I pass the value of these checked buttons to my backend?

I have a few radio buttons a user can check to add search parameters to their search query. When I make the post request to my server, I am getting 'on' as the value.

Here's a snippet of the HTML:

<form method="post" action="/">
  <input type="radio" name="sushi">
  <label for="sushi">Sushi</label>
  <button type="submit">Enter</button>
</form>

server.js:

app.post('/', (req, res) => {
     const request = req.body;
     let sushi = request.sushi;

    console.log(sushi); //returns 'on'
});

I can get the value from a text input field using this method with no problem, but the radio buttons do not work this way. They only return a value of on if the radios are clicked on submit. I know I am missing something, but I'm having trouble finding any solutions that don't relate to the frontend and JQuery.

How can I pass the value of these checked buttons to my backend?

Share Improve this question asked Feb 25, 2018 at 20:39 maison.mmaison.m 8734 gold badges21 silver badges40 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 5

Add a value property on your radio input:

<form method="post" action="/">
  <input type="radio" value="sushi" name="sushi">
  <label for="sushi">Sushi</label>
  <button type="submit">Enter</button>
</form> 

this is what gets posted

发布评论

评论列表(0)

  1. 暂无评论