jQuery 将文本字段的值从不同的表单复制到另一个表单的文本字段
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15071218/
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 Copy text field's value from a different form into another form's text field
提问by Claudio Delgado
I have two forms.
我有两种形式。
I want to copy the value of one of the text fields in form 1 into another in form 2.
我想将表单 1 中的一个文本字段的值复制到表单 2 中的另一个文本字段中。
Text field names and ids are different.
文本字段名称和 id 不同。
How can I achieve this?
我怎样才能做到这一点?
This didn't work:
这不起作用:
document.getElementById('name').value = document.getElementById('user').value;
Thanks!
谢谢!
回答by Corey
If you're asking for jQuery you could try:
如果您要求使用 jQuery,您可以尝试:
$("#name").val($("#user").val());
回答by LiamB
$(document).ready(function()
{
$('#btn1').click(function()
{
$('#field2').val($('#field1').val());
});
});
回答by Tarun Kumar
To get the value using jquery .it has to be done this way . Also make sure you have given id to the input fields .
要使用 jquery 获取值,必须以这种方式完成。还要确保您已为输入字段提供了 id。
like this: <input type='text' name='user' id='user' >
像这样: <input type='text' name='user' id='user' >
Then only this code will work properly
那么只有这段代码才能正常工作
$(document).ready(function()
{
$('#buttonElement').click(function()
{
$('#name').val($('#user').val());
});
});