从html页面的node.js中使用Express获取下拉列表值 [英] Get dropdown value using Express in node.js from html page

查看:170
本文介绍了从html页面的node.js中使用Express获取下拉列表值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用快速开发一个快速的node.js应用程序,我是NODE的新手。对于页面,我只是使用简单的html。



基本上我有一个表单如下:

 < form id =tableFormaction =getJson> 
< select class =selectpickerdata-style =btn-infoname =selectpicker>
< optgroup label =选择表>
< option name =value =0>选择表< / option>
< option name =table1value =1>表1< / option>
< option name =table2value =2>表2< / option>
< option name =table3value =3>表3< / option>
< / optgroup>
< / select>
< / form>

基本上,我需要获取一次完成的值,我需要它传递一个 app.get()调用,但我的问题是如何获取值并调用API?

 code> var express = require('express'),
app = express();

app.use(express.bodyParser());
//只有一个页面可以使用res.sendfile来渲染页面,
//包含下拉列表...
app.get('/',function(req,res ){
res.sendfile('views / index.html');
});

app.get('/ getJson',function(req,res){
console.log(req.body。);
});

app.listen(process.env.PORT);

所以我需要调用 getJson()



Cheers!

解决方案

您需要以某种方式提交表单。最简单的方法是使用提交按钮。您还需要将表单的方法,按照您的措辞,这听起来像您想要使用GET。



HTML

 < form id =tableFormaction =/ getJsonmethod =get> 
< select class =selectpickerdata-style =btn-infoname =selectpicker>
< optgroup label =选择表>
< option name =value =0>选择表< / option>
< option name =table1value =1>表1< / option>
< option name =table2value =2>表2< / option>
< option name =table3value =3>表3< / option>
< / optgroup>
< / select>
< input type =submit/>
< / form>

在服务器端,您需要解析get请求。你已经有了它来接收它,你只需要知道你在找什么。由于您的选择具有名称selectpicker,这是您将在此情况下使用的。



JavaScript

  var express = require('express'),
app = express();

app.use(express.bodyParser());

//只有一个页面可以使用res.sendfile来渲染包含下拉列表的页面
app.get('/',function(req,res){
res.sendfile('views / index.html');
});

app.get('/ getJson',function(req,res){
//如果没有显示,只需使用req.body查看实际正在传递的内容
console.log(req.body.selectpicker);
});

app.listen(process.env.PORT);

我没有完全测试这段代码,但它应该可以工作。


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:

 <form id="tableForm" action="getJson">
        <select class="selectpicker" data-style="btn-info" name="selectpicker">
            <optgroup label="Select Table">
              <option name="" value="0">Select table</option>
              <option name="table1" value="1">Table 1</option>
              <option name="table2" value="2">Table 2</option>
              <option name="table3" value="3">Table 3</option>
            </optgroup>
        </select>
    </form>

Basically, I need to get the value selected once done I need it to be passed a app.get() call but my questions is how do I get the value and call the API?

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 dropdowns ...
 app.get('/', function(req, res){
  res.sendfile('views/index.html');
});

app.get('/getJson', function (req, res) {
   console.log(req.body.);
});

app.listen(process.env.PORT);

So I need to call the getJson() with the value being passed in.

Cheers!

解决方案

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

<form id="tableForm" action="/getJson" method="get">
    <select class="selectpicker" data-style="btn-info" name="selectpicker">
        <optgroup label="Select Table">
            <option name="" value="0">Select table</option>
            <option name="table1" value="1">Table 1</option>
            <option name="table2" value="2">Table 2</option>
            <option name="table3" value="3">Table 3</option>
        </optgroup>
    </select>
    <input type="submit" />
</form>

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.

这篇关于从html页面的node.js中使用Express获取下拉列表值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆