Get dropdown value using Express in node.js from html page

前端 未结 3 1413
花落未央
花落未央 2021-01-31 12:08

I am developing a quick node.js app using express and I am a newbie to NODE. For pages I am just using plain html.

Basically I have a form as follows:

 &         


        
3条回答
  •  庸人自扰
    2021-01-31 13:06

    You need to submit the form somehow. The easiest way to do it would be with a submit button. You also need to put the method for the form, which by the way you phrased it it sounds like you're wanting to use GET.

    HTML

    On the server side you need parse out the get request. You already have it set up to receive it, you just need to know what you're looking for. Since your select has the name "selectpicker" that's what you'll use in this case.

    JavaScript

    var express = require('express'),
        app = express();
    
    app.use(express.bodyParser());
    
    // as only one page can use res.sendfile to render the page which will contain the drop   downs
    app.get('/', function (req, res) {
        res.sendfile('views/index.html');
    });
    
    app.get('/getJson', function (req, res) {
        // If it's not showing up, just use req.body to see what is actually being passed.
        console.log(req.body.selectpicker);
    });
    
    app.listen(process.env.PORT);
    

    I haven't fully tested this code, but it should work.

提交回复
热议问题