在 Node.js 中使用 post 请求获取选中的单选按钮的值
Get values of checked radio button with post request in Node.js
我有几个单选按钮,用户可以选中这些按钮来将搜索参数添加到他们的搜索查询中。当我向我的服务器发出 post 请求时,我得到 'on' 作为值。
这是 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'
});
我可以使用此方法毫无问题地从文本输入字段中获取值,但单选按钮无法以这种方式工作。如果在提交时单击收音机,它们只会 return 值 on
。我知道我遗漏了一些东西,但我找不到任何与前端和 JQuery.
无关的解决方案
如何将这些选中按钮的值传递到我的后端?
在您的收音机输入上添加一个值 属性:
<form method="post" action="/">
<input type="radio" value="sushi" name="sushi">
<label for="sushi">Sushi</label>
<button type="submit">Enter</button>
</form>
这是发布的内容
我有几个单选按钮,用户可以选中这些按钮来将搜索参数添加到他们的搜索查询中。当我向我的服务器发出 post 请求时,我得到 'on' 作为值。
这是 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'
});
我可以使用此方法毫无问题地从文本输入字段中获取值,但单选按钮无法以这种方式工作。如果在提交时单击收音机,它们只会 return 值 on
。我知道我遗漏了一些东西,但我找不到任何与前端和 JQuery.
如何将这些选中按钮的值传递到我的后端?
在您的收音机输入上添加一个值 属性:
<form method="post" action="/">
<input type="radio" value="sushi" name="sushi">
<label for="sushi">Sushi</label>
<button type="submit">Enter</button>
</form>
这是发布的内容