javascript 如何使用jquery将值传递给隐藏字段?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5433113/
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
how to pass a value to a hidden field using jquery?
提问by ceasor
<input type="hidden" id="name" name="hidden-new-field"/>
<form>
<input type="text" id="name-emails" />
<input id="send-btn" type="submit" class="button" value="SEND NOW" />
</form>
On button click I want to give the value of input type text to the value of hidden type
单击按钮时,我想将输入类型文本的值赋予隐藏类型的值
回答by diEcho
There are few mistakes in your form
你的表格几乎没有错误
- there is no
name
attribute on input type text id
of Hiddenelement should not be name(leading confusion)- Hidden field should be inside
form
tag
- 输入类型文本没有
name
属性 id
的隐藏元素不应该是名(领先混乱)- 隐藏字段应该在
form
标签内
i would prefer like this
我更喜欢这样
PHP
PHP
<form>
<input type="text" id="email" name="email"/>
<input type="hidden" id="hidden_email" name="hidden_email"/>
<input type="submit" id="send_btn" class="button" value="SEND NOW" />
</form>
jQuery
jQuery
$("form").bind('submit',function(e){
e.preventDefault();
var formEmail=$("input[name=email]").val();
$("input[type=hidden][name=hidden_email]").val(formEmail);
});
DEMO
演示
回答by mattsven
$("form").submit(function(e){
e.preventDefault();
$("#name").val("new_hidden_value");
});
回答by Andrei Andrushkevich
var value = $("#name").val();
$("#name").val(value);
回答by Kon
$('#send-btn').click(function() {
$('#name').val($('#name-emails').val());
});
回答by Patrick Karcher
$(document).ready(function() {
$("#send-btn").click(function(){
$("#name").val($("#name").val(name-emails)));
});
});
Note that the above code will still submit the form afterwards, which I assume is what you want.
请注意,上面的代码仍然会在之后提交表单,我认为这是您想要的。
回答by Billy Moon
http://jsfiddle.net/billymoon/h97M6/
http://jsfiddle.net/billymoon/h97M6/
$('form').bind('submit',function() {
$('#name').val($('#name-emails').val())
alert($('#name-emails').val())
return false
})
回答by Gaurav
$('input#send-btn').click(function(){
$('input#name').val($('input#name-emails').val());
return true;
});