javascript 在提交时向表单添加外部输入值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12136284/
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
Add an external input value to form on submit
提问by Jürgen Paul
I have a typical form:
我有一个典型的形式:
<form action="" accept-charset="utf-8" method="post">
<textarea name="content"></textarea>
</form>
and an not-inside-a-form element:
和一个 not-inside-a-form 元素:
<input type="password" name="password">
How do I add the value of password into the form when I submit the form?
提交表单时如何将密码的值添加到表单中?
$('form').submit(function(){
//hmmm
});
回答by Vins
Create a hidden field in the form and copy the password field value to that field on submit. Like this.
在表单中创建一个隐藏字段,并在提交时将密码字段值复制到该字段。像这样。
<form action="" accept-charset="utf-8" method="post">
<textarea name="content"></textarea>
<input type="hidden" name="password" id="ps">
</form>
<input type="password" name="password" id="ps1">
And in on submit function.
并在提交功能。
$('form').submit(function(){
$('input#ps').val($('input#ps1').val());
return true;
});
回答by zzzzBov
The not-yet-supported-but-HTML5-compliant way to do this "correctly" is to give your <input>
element a [form]
attribute:
尚未支持但符合 HTML5 标准的“正确”执行此操作的方法是为您的<input>
元素提供一个[form]
属性:
<form id="foo">
...stuff...
</form>
<input type="password" id="bar" form="foo" />
Eventually you may be able to use this as a solution, but until more browsers support the [form]
attribute, you'll have to polyfill it with JavaScript.
最终您可能可以使用它作为解决方案,但在更多浏览器支持该[form]
属性之前,您必须使用 JavaScript 对其进行 polyfill。
回答by ahren
$('form').submit(function(){
var password = $('input[type="password"]');
password.appendTo($(this));
//or $(this).append(password);
});
回答by Julian
include a hidden input element inside of the form, on the outer input's change event, assign the inner input the outer inputs value.
在表单内部包含一个隐藏的输入元素,在外部输入的更改事件中,将内部输入分配给外部输入值。
<form action="" accept-charset="utf-8" method="post">
<textarea name="content"></textarea>
<input type="hidden" id="inner" />
</form>
<input type="password" name="password" >
<script type="text/javascript">
$(function(){
$('#outer').change(function(){
$('#inner').val($(this).val());
});
});
</script>
回答by insomiac
Use hidden input field inside form like this:
在表单中使用隐藏的输入字段,如下所示:
ex : <input type="hidden" name="pass">
前任 : <input type="hidden" name="pass">
When you submit the form like this :
当您像这样提交表单时:
$('form').submit(function(){
<!-- Save the value of password into the hidden field
Note : Password here should just be one field in that page -->
$('input[name="pass"]').val($('input[type="password"]').val());
return true;
});
});