how to pass a value to a hidden field using jquery?
<开发者_如何学运维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
There are few mistakes in your form
- there is no
name
attribute on input type text id
of Hidden element should not be name (leading confusion)- Hidden field should be inside
form
tag
i would prefer like this
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
$("form").bind('submit',function(e){
e.preventDefault();
var formEmail=$("input[name=email]").val();
$("input[type=hidden][name=hidden_email]").val(formEmail);
});
DEMO
$("form").submit(function(e){
e.preventDefault();
$("#name").val("new_hidden_value");
});
$('input#send-btn').click(function(){
$('input#name').val($('input#name-emails').val());
return true;
});
$('#send-btn').click(function() {
$('#name').val($('#name-emails').val());
});
$(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.
http://jsfiddle.net/billymoon/h97M6/
$('form').bind('submit',function() {
$('#name').val($('#name-emails').val())
alert($('#name-emails').val())
return false
})
var value = $("#name").val();
$("#name").val(value);
精彩评论