0
我想將選擇框的值發送到我的NodeJS服務器,並使用onchange()函數使用socket.io。使用OnChange在Socket.io上發送數據
現在,我可以用下面的代碼發送按鈕顯示;
server.js;
var app = require('express')();
var http = require('http').Server(app);
var io = require('socket.io')(http);
app.get('/', function(req, res) {
res.sendFile(__dirname + '/index.html');
});
io.on('connection', function(socket) {
socket.on('socketdeneme', function(msg) {
console.log("Message received: " + msg);
});
});
http.listen(2000, function() {
console.log('listening on *:2000');
});
的index.html;
<!doctype html>
<html>
<head>
<title>Socket.IO chat</title>
<script src="/socket.io/socket.io.js"></script>
<script src="https://code.jquery.com/jquery-1.11.1.js"></script>
<script type="text/javascript">
$(function() {
var socket = io();
$('form').submit(function() {
socket.emit('socketdeneme', $('#m').val());
});
});
</script>
</head>
<body>
<form action="">
<select id=m multiple>
<option value="dnm">dnm</option>
<option value="dnm1">dnm1</option>
<option value="dnm2">dnm2</option>
</select>
<button>Send</button>
</form>
</body>
</html>
在另一方面,下面的代碼發送數據與的onchange()函數,但我無法使用socket.io。
<script type="text/javascript">
function submitformfabrika() {
document.formsubmitfabrika.submit();
}
</script>