jQuery - 将值从一个输入传递到另一个输入
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5896287/
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
jQuery - passing value from one input to another
提问by sipher_z
I have a form, with several input fields that are title
, name
, address
etc
我有一个表格,有几个输入字段是title
,name
,address
等
What I want to do, is to get these values and 'put them' into values of other input fields. For example
我想要做的是获取这些值并将它们“放入”其他输入字段的值中。例如
<label for="first_name">First Name</label>
<input type="text" name="name" />
<label for="surname">Surname</label>
<input type="text" name="surname" />
<label for="firstname">Firstname</label>
<input type="text" name="firstname" disabled="disabled" />
So If I enter John
in the first_name
field, then the value of firstname
will also be John
.
因此,如果我John
在该first_name
字段中输入,则 的值也firstname
将为John
。
Many thanks
非常感谢
回答by Richard Dalton
Assuming you can put ID's on the inputs:
假设您可以将 ID 放在输入上:
$('#name').change(function() {
$('#firstname').val($(this).val());
});
Otherwise you'll have to select using the names:
否则,您必须选择使用名称:
$('input[name="name"]').change(function() {
$('input[name="firstname"]').val($(this).val());
});
回答by Russ Clarke
It's simpler if you modify your HTML a little bit:
如果您稍微修改一下 HTML,那就更简单了:
<label for="first_name">First Name</label>
<input type="text" id="name" name="name" />
<label for="surname">Surname</label>
<input type="text" id="surname" name="surname" />
<label for="firstname">Firstname</label>
<input type="text" id="firstname" name="firstname" disabled="disabled" />
then it's relatively simple
那么就比较简单了
$(document).ready(function() {
$('#name').change(function() {
$('#firstname').val($('#name').val());
});
});
回答by Kon
Add ID attributes with same values as name attributes and then you can do this:
添加具有与名称属性相同值的 ID 属性,然后您可以执行以下操作:
$('#first_name').change(function () {
$('#firstname').val($(this).val());
});
回答by Carlos Ferrer
Get input1 data to send them to input2 immediately
获取 input1 数据以立即将它们发送到 input2
<div>
<label>Input1</label>
<input type="text" id="input1" value="">
</div>
</br>
<label>Input2</label>
<input type="text" id="input2" value="">
<script type="text/javascript">
$(document).ready(function () {
$("#input1").keyup(function () {
var value = $(this).val();
$("#input2").val(value);
});
});
</script>