使用表单提交(jQuery)上的文本字段的值填充隐藏的表单元素值 [英] Populate hidden form element value with the value of a text field on form submit (jQuery)

查看:106
本文介绍了使用表单提交(jQuery)上的文本字段的值填充隐藏的表单元素值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用提交时在表单的单行文本字段中输入的值自动填充隐藏的表单元素.使用jQuery如何完成?感谢您的任何建议.

I want to automatically populate a hidden form element with the value of what is entered in a single line text field in a form when submitted. How would this be done with jQuery? Thanks for any suggestions.

<form>
<input type="hidden" id="username" name="username" value="" />
<input type="text" id="email" name="email" value="some.email@typed.here.on.webpage.com" />
<input type="submit" value="Submit" class="submit" />
</form>

推荐答案

我发现此方法在所有浏览器中都能很好地工作:

I've found this method to work well across browsers:

<html>
<head>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.2.6/jquery.min.js"></script>
<script type="text/javascript">
function submitMyForm(){
    $('#username').val($('#email').val());
    $('#myform').submit();
}
</script>
<title>Test</title>
</head>
<body>
<form id="myform" action="http://www.example.com" method="post">
<input type="hidden" id="username" name="username" value="" />
<input type="text" id="email" name="email" value="some.email@typed.here.on.webpage.com" />
</form>
<button class="submit"  onclick="submitMyForm()">Submit</button>
</body>
</html>

这篇关于使用表单提交(jQuery)上的文本字段的值填充隐藏的表单元素值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆