首页 > 解决方案 > Javascript,通过 2 个事件更新值并连接

问题描述

一个简单的问题。我有 2 个控制更改事件。

我有第一个事件

ctrlProducto_tipo.on  ('change', function(e){
  if (this.getValue()== 'Up'){
    var strProducto = 'U';}
  if (this.getValue()== 'Down'){
    var strProducto = 'D';}
  if (this.getValue()== 'Left'){
    var strProducto = 'L';}
  if (this.getValue()== 'Right'){
    var strProducto = 'R';}
  ctrlTest1.setValue(strProducto);
});

当我选择“向下”时,我text1.textfield的“D”中有

在我的第二个事件中显示Test2.textbox

ctrlEspesor.on  ('keyup', function(e){
if (this.getValue()!== ''){
var strEspesor = ctrlEspesor.getValue();}
ctrlTest2.setValue(strEspesor);

当我输入“4”时,我有“4”。

2个事件有效。但现在我尝试连接strProductostrEspesor到(在&ctrlTest3.textbox之前没有显示)。ctrlTest1ctrlTest2

但是每次用户更改两个值之一时,我都需要ctrlText3即时更新。

我试试这个,没有成功。

var valueFinal = strProducto.concat(strEspesor);
ctrlTest3.setValue(valueFinal);

示例ctrlTest3:'D4'

欢迎提供大帮助,谢谢......

标签: javascripteventsconcatenation

解决方案


我试图理解你的意思。在codepen检查这个。

Javascript

var select = document.getElementById('select');
var txt1 = document.getElementById('text1');
var txt2 = document.getElementById('text2');
var txt3 = document.getElementById('text3');

select.addEventListener('change', (e)=>{
  txt1.value = e.target.value;
  txt3.value = txt1.value + txt2.value;
})

txt2.addEventListener('keyup', (e)=>{
  txt3.value = txt1.value + txt2.value;
})

HTML 部分

  <select id="select">
    <option value="u">Up</option>
    <option value="d">Down</option>
    <option value="l">Left</option>
    <option value="r">Right</option>
  </select><br>
  <input type="text" id="text1"><br>
  <input type="text" id="text2"><br>
  <input type="text" id="text3">

推荐阅读