javascript - 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个事件有效。但现在我尝试连接strProducto
和strEspesor
到(在&ctrlTest3.textbox
之前没有显示)。ctrlTest1
ctrlTest2
但是每次用户更改两个值之一时,我都需要ctrlText3
即时更新。
我试试这个,没有成功。
var valueFinal = strProducto.concat(strEspesor);
ctrlTest3.setValue(valueFinal);
示例ctrlTest3
:'D4'
欢迎提供大帮助,谢谢......
解决方案
我试图理解你的意思。在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">
推荐阅读
- javascript - 如何设置下载 API 的文件路径以记住上次位置
- r - 如何合并 2 个数据框?
- c# - 重启后需要自动启动的 C# 后台应用程序无法在 Windows 7 上运行(即使在添加清单后)
- ruby-on-rails - Heroku Rails 4 资产不加载
- javascript - 从 react-router-bootstrap 使用 LinkContainer 时测试失败
- graphql - 如何从 graphql-ruby 中的 InputObject 类型继承参数?
- google-cloud-dataflow - Google Cloud Dataflow 中的自动扩缩
- php - 在 WP_Query 中从 Woocommerce 产品预订中获取 sku
- javascript - 调用函数时未定义,但并非每次调用都发生
- django - 根据文档设置 django-comments-xtd 时,“str”对象没有属性“_meta”错误