如何将JavaScript中的两个变量合并为一个?

时间:2019-05-14 03:07:43

标签: javascript jquery node.js chatbot

我试图连接两个变量,以便将两个形式的框(首字母和消息)发送到我的chatbot服务器,并在浏览器中显示它们。

  var text = $('#message').val(); 
  var initials =$('#initials').val();
//I need to combine the two above variables so I can emit one distinct variable
  socket.emit('message', text);
  $('#message').val('');
  $('#initials').val('');
  return false;
});

socket.on('message', function (msg) {
  $('<li>').text(msg).appendTo('#history');
});````

2 个答案:

答案 0 :(得分:2)

要么在调用emit之前从输入的值中加入它们,要么在调用emit时或在套接字的方法中(在emit之后)将它们作为额外参数加入


    this.weightdevicesService.getweightdevicesbypatient(this.userId)
      .subscribe((weightdevices: Weightdevices[]) => {
        this.weightdevices = weightdevices;
      });
    console.log(this.weightdevices);
    const source = from(this.weightdevices); //changes to observable 
    const hfilter = source.pipe(pluck('height')); // pluck to filter out data to [ '55.9', '57.2' ]
    console.log(hfilter);
    const hfilterob = from(hfilter);  // ensures it is observable - this dosent seem to make a difference?
    const weightdata = hfilter.map(Number); // convert  [ '55.9', '57.2' ] to  [ 55.9, 57.2 ]
    console.log(weightdata);

或仅作为参数加入

  var text = $('#message').val() + $('#initials').val();
  ...
  socket.emit('message', text);

或将它们分开并添加首字母作为额外参数

...
  socket.emit('message', text + initials);
...

答案 1 :(得分:0)

尝试使用运算符“ +”:

socket.emit('message', text + initials);