Javascript 用另一个输入字段值更新隐藏的输入字段值?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14709637/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Update hidden input field value with another input field value?
提问by Rooz Far
I am trying to update one hidden input field with another visible input field using javascript...
我正在尝试使用 javascript 用另一个可见的输入字段更新一个隐藏的输入字段...
but my code doesn't work for some reason!!
但我的代码由于某种原因不起作用!!
This is my javascript:
这是我的javascript:
<script language="javascript">
function process1() {
document.getElementById("sum_total").value = (document.getElementById("my-item-price").value);
}
</script>
And This Is My form code:
这是我的表单代码:
<form><input name="A1" type="text" class="A1" id="A1"/></form>
<form><input type="hidden" id="A2" name="A2" value="process1() "/></form>
Update
更新
<script language="javascript">
function process1() {
document.getElementById("sum_total").value = (document.getElementById("my-item-price").value);
}
</script>
回答by Tomas Nekvinda
The code you have is bad. You haven′t got ids you are looking for in javascript.
你的代码很糟糕。您还没有id在 javascript 中找到您正在寻找的 s。
Try this:
尝试这个:
<script language="javascript">
function process1(showed) {
document.getElementById("A2").value = showed.value;
}
</script>
<form><input name="A1" type="text" class="A1" onChange="process1(this)"/></form>
<form><input type="hidden" id="A2" name="A2" value="5" /></form>
回答by ElPedro
I think the most reliable will be to use onKeyUp rather than onChange or mouse events (onBlur etc.). The following updates the hidden field as each character is entered (I have un-hidden A2 so you can see what is happening).
我认为最可靠的是使用 onKeyUp 而不是 onChange 或鼠标事件(onBlur 等)。以下在输入每个字符时更新隐藏字段(我已取消隐藏 A2,因此您可以看到发生了什么)。
<html>
<head>
<script language="javascript">
function process1() {
document.getElementById("A2").value = (document.getElementById("A1").value);
}
</script>
</head>
<body>
<form><input name="A1" type="text" class="A1" id="A1" onKeyUp="process1()" /></form>
<form><input id="A2" name="A2" value="" /></form>
</body>
</html>
回答by Emre Senturk
You should bind an event to update the value. For example onkeyupevent
document.getElementById("A1").onkeyup=function()
{
document.getElementById("A2").value = this.value
};
您应该绑定一个事件来更新值。例如 onkeyupevent
document.getElementById("A1").onkeyup=function()
{
document.getElementById("A2").value = this.value
};
回答by ATOzTOA
Try this: Live Demo
试试这个:现场演示
HTML
HTML
<form><input name="A1" type="text" class="A1" id="A1"/></form>
<form><input type="hidden" id="A2" name="A2" value=""/></form>
<button onClick="process1();">Update</button>
JS
JS
function process1() {
document.getElementById("A2").value = document.getElementById("A1").value;
alert(document.getElementById("A2").value);
}
回答by Ryan Beaulieu
I changed the value to blank and gave the input an onmouseout (you can use another type if you want) to execute the function and swap values.
我将值更改为空白并给输入一个 onmouseout(如果需要,您可以使用其他类型)来执行函数和交换值。
<script language="javascript">
function process1() {
document.getElementById("A2").value = document.getElementById("A1").value;
}
</script>
</head>
<body>
And This Is My form code:
<form><input name="A1" type="text" class="A1" id="A1"/></form>
<form><input type="text" id="A2" name="A2" value="" onmouseout="process1()" /></form>
回答by pedak
<script language="javascript">
function process1() {
document.getElementById("A1").value = (document.getElementById("A2").value);
}
</script>
And This Is My form code:
<form><input name="A1" type="text" class="A1" id="A1"/></form>
<form><input type="hidden" id="A2" name="A2" value="process1() "/></form>
<div onClick="process1()">doit</div>
does work
确实有效

