【问题标题】:parsing jade forms with node.js用 node.js 解析翡翠表格
【发布时间】:2015-06-11 09:57:04
【问题描述】:

我不确定如何从下拉列表中获取用户选择的项目。我的玉码很短:

extends layout

block content
  p This is the CSV page
  p <select>
    <option value="domain">Domain</option>
    <option value="test">Test</option>
    <option value="random">Random</option>
    <option value="databases">Databases</option>
  </select>
  form(action="/csvuploader", method="post", enctype="multipart/form-data")
    label(for="payload") Select a file to upload:
    input#payload(type="file" name="myFile" accept="/*")
    br
    button#upload Upload

在文件上传表单的“操作”部分引用的 /csvuploader 文件中使用一些 busboy 方法,我已成功保存文件。我现在如何访问从下拉列表中选择的值?

抱歉,如果这听起来是一个非常愚蠢的问题,我是 node.js 和表单的新手。谢谢。

【问题讨论】:

    标签: javascript node.js forms parsing pug


    【解决方案1】:

    您需要将&lt;select&gt; 移动到&lt;form&gt; 元素内部,并为其命名:

    form(action="/csvuploader", method="post", enctype="multipart/form-data")
      p This is the CSV page
      p
        select(name = "dropdown")
          option(value="domain") Domain
          option(value="test") Test
          option(value="random") Random
          option(value="databases") Databases
      label(for="payload") Select a file to upload:
      input#payload(type="file" name="myFile" accept="/*")
      br
      button#upload Upload
    

    之后,您可以从req.body.dropdown 中读取下拉列表的值。

    【讨论】:

    • 感谢您的回答,但是 req.body.dropdown 在控制台中返回 undefined。这和我正在使用的 busboy 有什么关系吗?
    • @user2412643 我假设您将使用express-busboy,它将填充req.body。如果你使用“plain” busboy,你需要使用busboy.on('field', ...)
    • 我正在使用 connect-busboy。唯一的问题是,我已经有req.busboy.on('file', ... 来保存从表单传递的文件。这会使事情复杂化吗? @robertklep
    • @user2412643 不,这不会是一个问题
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-01
    • 1970-01-01
    相关资源
    最近更新 更多