我在 JS 中写了一个小函数,它将从文本字段 1 中获取数字输入并计算它的 2/3 的纵横比并将此值写入文本字段 2。现在我想要文本字段 2 具有相同的功能,但具有比例为 3/2,因此无论用户输入什么值,我都希望其他文本文件自动填充计算值。
这是我的代码,用于输入文本文件 1 并在文本文件 2 中显示其中的 2/3:
function change()
{
var ex1Val = (document.getElementById("ex1")).value;
if (ex1Val < "0" || "9" < ex1Val)
{
//(document.getElementById("ex1")).value = "";
(document.getElementById("ex1")).focus();
return false;
}
else
{
(document.getElementById("ex2")).value = ((ex1Val * 2) / 3);
}
};
我尝试复制函数并编辑 ID 和宽高比,但这不起作用。
如果不是 JS,Jquery 也可以。
最佳答案
您可以使用 onInput="change(this.id)"
事件并检查 id 并计算您的纵横比。
function change(id) {
var ex1Val = (document.getElementById(id)).value;
if (id == "ext1") {
(document.getElementById("ext2")).value = ((ex1Val * 2) / 3);
} else if (id === "ext2") {
(document.getElementById("ext1")).value = ((ex1Val * 3) / 2);
}
};
<input id="ext1" onInput="change(this.id)" />
<input id="ext2" onInput="change(this.id)" />
关于javascript - 如何将值从文本字段 1 写入文本字段 2,反之亦然,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53922765/