jQuery 将 Javascript 输入隐藏字段的值设置为在另一个输入文本字段中输入的值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15312564/
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
Set the value of a Javascript input hidden field to that of a value entered in another input text field
提问by Raghu
I have an input text field (name: qtyText
) to which a user enters a value. I would like to set this value as the value for another hidden field (name: qtyTextHidden
) using JavaScript. How can I go about it?
我有一个输入文本字段 ( name: qtyText
),用户可以在其中输入一个值。我想name: qtyTextHidden
使用 JavaScript将此值设置为另一个隐藏字段 ( )的值。我该怎么办?
HTML
HTML
<input name = "qtyText" type = "textbox" size = "2" value = "" />
<input type="hidden" value = "" name="qtyTextHidden"/>
My efforts to set the field value using JS work, but I am unable to send the value to the servlet. So I am attempting to directly set the value using a function and then try and send it to the servlet. I would like to have a value = someJSFunction()
kind. The function needs to trigger upon "onChange" in the "qtyText" input field.
我努力使用 JS 设置字段值,但我无法将该值发送到 servlet。因此,我尝试使用函数直接设置该值,然后尝试将其发送到 servlet。我想要value = someJSFunction()
一种。该函数需要在“qtyText”输入字段中的“onChange”时触发。
回答by
Using jQuery:
使用jQuery:
$(document).ready(function() {
$('input:text[name="qtyText"]').keyup(function() {
$('input:hidden[name="qtyTextHidden"]').val( $(this).val() );
});
});
Using JavaScript:
使用 JavaScript:
window.onload = function() {
if(document.readyState == 'complete') {
document.getElementsByTagName('input')[0].onkeyup = function() {
document.getElementsByTagName('input')[1].value = this.value;
};
}
}:
回答by David says reinstate Monica
I'd suggest:
我建议:
function updateHidden(valueFrom, valueTo) {
valueTo.value = valueFrom.value;
}
var inputs = document.getElementsByTagName('input'),
textInputs = [],
hiddenInputs = [],
refersTo;
for (var i = 0, len = inputs.length; i < len; i++) {
switch (inputs[i].type) {
case 'hidden':
hiddenInputs.push(inputs[i]);
break;
case 'text':
default:
textInputs.push(inputs[i]);
break;
}
}
for (var i = 0, len = textInputs.length; i < len; i++) {
refersTo = document.getElementsByName(textInputs[i].name + 'Hidden')[0];
if (refersTo !== null) {
textInputs[i].onchange = function () {
updateHidden(this, document.getElementsByName(this.name + 'Hidden')[0]);
};
}
}
Incidentally: there is notype="textbox"
. At all. Ever. Anywherein HTML, not even in HTML 5: it's type="text"
. The only reason it workswith type="textbox"
is because browsers are ridiculously forgiving and, if the type
isn't understood, it defaultsto type="text"
.
顺便说一句:没有type="textbox"
。在所有。曾经。在 HTML 中的任何地方,甚至在 HTML 5 中都没有:它是type="text"
. 它的唯一原因,工作与type="textbox"
是因为浏览器是可笑的宽容和,如果type
是不理解,它的默认值来type="text"
。
回答by Roman C
To set the value of the hidden field the easiest way is to use the javascript function that to pass three parameters from
, to
, and form
names:
要设置隐藏字段的最简单的方法是使用javascript函数的值传递三个参数from
,to
和form
名称:
<script type="text/javascript">
function someJSFunction(form, from, to){
var el_from=form[from], el_to=form[to];
el_to.value=el_from.value;
return el_to.value;
}
</script>
<form name="myform">
<input name = "qtyText" type = "textbox" size = "2" value = "" onchange="someJSFunction(myform, 'qtyText', 'qtyTextHidden')"/>
<input type="hidden" value = "" name="qtyTextHidden"/>