You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Node.js使用Formidable无法识别选中单选按钮的问题咨询

解决Formidable解析单选按钮时总是返回{"clean":"on"}的问题

嘿,我一眼就看出问题在哪啦!你的单选按钮没有设置value属性,这是HTML表单的一个小坑——如果radio标签不指定value,浏览器默认会把选中项的值设为"on",不管你选的是哪一个选项,所以Formidable解析出来自然都是{"clean":"on"}。

解决方案:给单选按钮添加value属性

只需要给每个<input type="radio">加上对应的value,就能让Formidable正确识别你选中的选项了。修改后的表单代码如下:

<form action="/upload" enctype="multipart/form-data" method="post">
  <div class="form-group text-center">
    <div class="radio">
      <label>
        <input type="radio" name="clean" value="clean" checked="checked">Clean
      </label>
    </div>
    <div class="radio">
      <label>
        <input type="radio" name="clean" value="waste">Waste
      </label>
    </div>
    <input class="form-control" type="file" name="file" accept=".xlsx">
    <input class="form-control btn btn-primary" type="submit" value="Upload">
  </div>
</form>

验证效果

修改之后,再用Formidable解析请求:

form.parse(req, (err, fields, files) => {
  if (err) {
    // 处理错误
    return;
  }
  console.log(fields.clean); // 选中Clean时输出"clean",选中Waste时输出"waste"
});

这样就能准确区分用户选中的是哪个单选按钮啦!

内容的提问来源于stack exchange,提问作者Owain Williams

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.27 07:23:19