两个文本框事件Javascript
Two Text Box Events Javascript
我在编写两个文本框事件时遇到了一些问题,希望得到一些帮助。。。
我有以下内容:
<body>
<form action="#">
<input type="text" id="begin" />
<select id "toLeftBox">
<option value = "a">Apple</option>
<option value = "b">Blueberry</option>
<option value = "c">Cherry</option>
</select>
=
<input type="text" id="done" />
<select id= "toRightBox">
<option value="a">Apple</option>
<option value="b">Blueberry</option>
<option value="c">Cherry</option>
</select>
<p id = "leftWeight"></p>
<p id = "rightWeight"></p>
</form>
<script>
var lftchoice = document.getElementById('begin'),
rgtchoice = document.getElementById('done');
newLeft = document.getElementById("toLeftBox");
newRight = document.getElementById("toRightBox");
newValRight = function(){
var tempR = document.getElementById("toRightBox").value;
if(tempR == "a"){
rgtchoice.value = lftchoice.value * 1;
}else if(tempR == "b"){
rgtchoice.value = lftchoice.value * 2;
}else if(tempR == "c"){
rgtchoice.value = lftchoice.value * 3;
}
}
newValLeft = function(){
var tempL = document.getElementById("toLeftBox").value;
if(tempL == tempR){
lftchoice.value = rgtchoice.value*1;
}else if(tempL == "b"){
lftchoice.value = rghtchoice.value * 2;
}else if(tempL == "c"){
lftchoice.value = rgtchoice.value * 3;
}
}
lftchoice.onkeyup = newValRight;
rgtchoice.onkeyup = newValLeft;
newLeft.onchange = newValLeft;
newRight.onchange = newValRight;
</script>
</body>
我有两个文本框。左侧文本框("开始")将接受"所选项目"的权重。但是右边的框('done')应该根据用户从下拉文本列表中选择的内容来更改数值。在左侧框中输入值后,右侧框将正确更新,同时更改下拉列表中的项目。然而,我希望它能双向运行。例如:
Left 23 [Apple] = Right 23 [Apple]
Left 23 [Apple] = Right 46 [Blueberry] (if I change to blueberry on right list)
Left 46 [Blueberry] = Right 46 [Blueberry] (if I keep the right, and just change lft list)
简单地说,我希望两个文本框都更新到与对面框上所选水果/等价物相关的正确值。
感谢
您在代码中犯了几个语法错误,并试图访问一些超出范围的变量。您在变量名称中也出现了拼写错误。
是你代码的工作小提琴
html代码
<form action="#">
<input type="text" id="begin" />
<select id="toLeftBox">
<option value = "a">Apple</option>
<option value = "b">Blueberry</option>
<option value = "c">Cherry</option>
</select>
=
<input type="text" id="done" />
<select id="toRightBox">
<option value="a">Apple</option>
<option value="b">Blueberry</option>
<option value="c">Cherry</option>
</select>
<p id = "leftWeight"></p>
<p id = "rightWeight"></p>
</form>
javascript
var lftchoice = document.getElementById('begin'),
rgtchoice = document.getElementById('done'),
newLeft = document.getElementById("toLeftBox"),
newRight = document.getElementById("toRightBox");
var tempL;
var tempR;
newValRight = function(){
tempR = document.getElementById("toRightBox").value;
if(tempR == "a"){
rgtchoice.value = lftchoice.value * 1;
}else if(tempR == "b"){
rgtchoice.value = lftchoice.value * 2;
}else if(tempR == "c"){
rgtchoice.value = lftchoice.value * 3;
}
}
newValLeft = function(){
tempL = document.getElementById("toLeftBox").value;
if(tempL == tempR){
lftchoice.value = rgtchoice.value*1;
}else if(tempL == "b"){
lftchoice.value = rgtchoice.value * 2;
}else if(tempL == "c"){
lftchoice.value = rgtchoice.value * 3;
}
}
lftchoice.onkeyup = newValRight;
rgtchoice.onkeyup = newValLeft;
newLeft.onchange = newValLeft;
newRight.onchange = newValRight;
我想这正是您所需要的。
<body>
<form action="#">
<input type="text" id="begin" onkeyup="k(1);" />
<select id="Fruitbox1" onclick="k(1);">
<option value = "3">Apple</option>
<option value = "1.5">Blueberry</option>
<option value = "1">Cherry</option>
</select>
=
<input type="text" id="done" onkeyup="k(0);"/>
<select id="Fruitbox2" onclick="k(0);">
<option value="3">Apple</option>
<option value="1.5">Blueberry</option>
<option value="1">Cherry</option>
</select>
<p id = "leftWeight"></p>
<p id = "rightWeight"></p>
</form>
<script>
function k(x){
var weightOfFruit1=document.getElementById("Fruitbox1")
var weightOfFruit2=document.getElementById("Fruitbox2")
var NumberofFruit1=document.getElementById("begin")
var NumberofFruit2=document.getElementById("done")
if(x==1)
{
TotalNumberofFruit2=(NumberofFruit1.value * weightOfFruit1.value)/weightOfFruit2.value
NumberofFruit2.value=parseInt(TotalNumberofFruit2)
}else
{
TotalNumberofFruit1=(NumberofFruit2.value * weightOfFruit2.value)/weightOfFruit1.value
NumberofFruit1.value=parseInt(TotalNumberofFruit1)
}
}
</script>
</body>
相关文章:
- 召回窗口加载事件 - javascript
- 动态创建OnClick事件Javascript
- 全局屏幕span onclick触发一个事件javascript
- 使用react js在跨域上执行事件javascript
- 在change事件javascript上动态添加行
- 在粘贴事件Javascript的输入字段中删除所有非数字字符
- 如何在多表单的输入框中使用输入事件javascript
- 无法查看特定信息.单击事件.Javascript
- 两个文本框事件Javascript
- 阻止鼠标滚轮滚动,但不阻止滚动条事件.JavaScript
- 从 onkeydown 事件 (Javascript) 获取当前用户输入
- “稍后提醒我”/“快速事件”JavaScript解决方案
- 更改我正在侦听的事件Javascript的对象的CSS
- setInterval 不允许同时单击事件 - javascript
- 对每种情况使用一个事件(JavaScript 开关)
- 仅创建第二次点击事件 Javascript
- 每 60 秒仅启动一次事件 (JavaScript)
- 如何连接来自不同文件的事件?Javascript/Backbone
- URL 更改事件 - JavaScript
- 无法在无头模式下使用 watir Webdriver 执行按钮元素的 onclick 事件 javascript