使用该选项的id获取select2的选定选项

时间:2015-10-01 13:53:43

标签: javascript jquery ajax jquery-select2

目前我在没有select2插件的情况下这样做但它可以工作,但我想使用select2插件

我在这里尝试做的是使用Select2插件获取所选选项,然后执行ajax调用

所以如果选择了一个选项,则会传递id并完成ajax调用

var fs = require('fs');

var http = require('http');
var https = require('https');

var privateKey  = fs.readFileSync(__dirname + '/cert/server.key', 'utf8');
var certificate = fs.readFileSync(__dirname + '/cert/server.cert', 'utf8');

var credentials = {key: privateKey, cert: certificate};
var express = require('express');
var app = express();

var httpServer = http.createServer(app);
var httpsServer = https.createServer(credentials, app);

httpServer.listen(80);
httpsServer.listen(443);

//Force https
app.use(function(req, res, next) {
  if(!req.secure) {
    return res.redirect(['https://', req.get('Host'), req.url].join(''));
  }
  next();
});

1 个答案:

答案 0 :(得分:0)

您将使用jquery检索select2输入的选定选项,就像您对普通选择输入所做的那样

http://jsfiddle.net/jEADR/2162/

<select id="someInputId" style="width:300px">
        <option value="AL">Alabama</option>
        <option value="Am">Amalapuram</option>
        <option value="An">Anakapalli</option>
        <option value="Ak">Akkayapalem</option>
        <option value="WY">Wyoming</option>
</select>


$("#someInputId").select2();

$('#someInputId').change(function(e){         
    console.log($(this).children(":selected").attr("value"));
});