Javascript question: How do I duplicate user input into a hidden field?

你。 提交于 2019-12-08 09:18:34

问题


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



来源:https://stackoverflow.com/questions/1202555/javascript-question-how-do-i-duplicate-user-input-into-a-hidden-field

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!