如何使用jquery将值传递给隐藏字段?

时间:2022-08-25 21:48:57
<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

单击按钮时,我想将输入类型文本的值赋予隐藏类型的值

7 个解决方案

#1


4  

There are few mistakes in your form

你的表格中有很少的错误

  • there is no name attribute on input type text
  • 输入类型文本没有name属性

  • id of Hidden element should not be name (leading confusion)
  • 隐藏元素的id不应该是名称(导致混淆)

  • 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

#2


2  

$("form").submit(function(e){
e.preventDefault();
$("#name").val("new_hidden_value");
});

#3


0  

$('input#send-btn').click(function(){
  $('input#name').val($('input#name-emails').val());
  return true;
});

#4


0  

$('#send-btn').click(function() {
  $('#name').val($('#name-emails').val());
});

#5


0  

$(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.

请注意,上面的代码仍然会在之后提交表单,我认为这是您想要的。

#6


0  

http://jsfiddle.net/billymoon/h97M6/

$('form').bind('submit',function() {
    $('#name').val($('#name-emails').val())
    alert($('#name-emails').val())
    return false
})

#7


0  

var value = $("#name").val();
$("#name").val(value);

#1


4  

There are few mistakes in your form

你的表格中有很少的错误

  • there is no name attribute on input type text
  • 输入类型文本没有name属性

  • id of Hidden element should not be name (leading confusion)
  • 隐藏元素的id不应该是名称(导致混淆)

  • 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

#2


2  

$("form").submit(function(e){
e.preventDefault();
$("#name").val("new_hidden_value");
});

#3


0  

$('input#send-btn').click(function(){
  $('input#name').val($('input#name-emails').val());
  return true;
});

#4


0  

$('#send-btn').click(function() {
  $('#name').val($('#name-emails').val());
});

#5


0  

$(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.

请注意,上面的代码仍然会在之后提交表单,我认为这是您想要的。

#6


0  

http://jsfiddle.net/billymoon/h97M6/

$('form').bind('submit',function() {
    $('#name').val($('#name-emails').val())
    alert($('#name-emails').val())
    return false
})

#7


0  

var value = $("#name").val();
$("#name").val(value);