Javascript question: How do I duplicate user input

2019-06-09 09:42发布

问题:

How can I pass input from a text-type input element into a hidden input element in the same form on a site that has jquery installed?

回答1:

Triggered when the text changes:

$("#text_id").change(function(){
  $("#hidden_id").val($("#text_id").val())
});

Triggered when the form submits:

$("#form_id").submit(function(){
  $("#hidden_id").val($("#text_id").val())
});

Check out other jQuery Events.



回答2:

If input_1 is the id of the input you want to populate, and input_2 the id you want to populate from, use:

$("#input_1").val($("#input_2").val())


回答3:

$("#id_of_hidden_input").val($("#id_of_text_input").val());


回答4:

Assuming that you have only one field of each type:

$('input[type=hidden]').val($('input[type=text]').val())

If you have more add IDs to selectors