简体   繁体   English

如何从 HTML 表单中收集所有值?

[英]How to collect all value from HTML form?

I'm currently trying to use jQuery to collect information from a HTML form, but I'm stuck.我目前正在尝试使用 jQuery 从 HTML 表单中收集信息,但我被卡住了。 Every time I console.log payload its empty and didn't capture the input values.每次我 console.log payload空的并且没有捕获输入值。

Questions:问题:

  • Why is payload empty at the end, after I input values into the form?在我将值输入到表单中后,为什么最后payload空? how to correct it?如何纠正?

  • Should I use document.ready for this or window.onload?我应该为此使用 document.ready 还是 window.onload?

Please any help is appreciated.请任何帮助表示赞赏。

Here is my last attempt jQuery:这是我最后一次尝试 jQuery:

 $(document).ready(function() { const ApplyOpeningPayloadBuilder = function() { let payload = { "fields": [] }; return { withKeyValue: function(key, value) { let param = {}; param.key = key; param.value = value; payload.fields.push(param); return this; }, withFile: function(key, encoded_data, filename) { let value = {}; value.encoded_data = encoded_data; value.file_name = filename; this.withKeyValue(key, value); return this; }, build: function() { return payload; } } } let encoded_file = "aGVsbG8gd29ybGQ="; let apply_for_an_opening_payload_builder = new ApplyOpeningPayloadBuilder(); $(".applicantForm input[type=text]").each(function() { apply_for_an_opening_payload_builder.withKeyValue(this.name, this.value); }); $(".applicantForm input[type=email]").each(function() { apply_for_an_opening_payload_builder.withKeyValue(this.name, this.value); }); $(".applicantForm input[type=tel]").each(function() { apply_for_an_opening_payload_builder.withKeyValue(this.name, this.value); }); $(".applicantForm input[type=url]").each(function() { apply_for_an_opening_payload_builder.withKeyValue(this.name, this.value); }); apply_for_an_opening_payload_builder.withFile("resume", encoded_file, "resume.txt"); let payload = apply_for_an_opening_payload_builder.build(); console.log("Log payload:", payload) });
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div class="container" id="json-response"> <div class="form-container"> <div class="header"> <h1>Application form</h1> </div> <form action="#" class="applicantForm"> <div class="form-group"> <div class="input-group"> <label for="First Name">First Name <span>*</span></label> <input type="text" name="First Name"> </div> <div class="input-group"> <label for="Last Name">Last Name <span>*</span></label> <input type="text" name="Last Name"> </div> </div> <div class="form-group"> <div class="input-group"> <label for="Email">Email <span>*</span></label> <input type="email" name="Email"> </div> <div class="input-group"> <label for="Phone">Phone <span>*</span></label> <input type="tel" name="Phone"> </div> </div> <div class="form-group"> <div class="input-group"> <label for="Resume">Resume <span>*</span></label> <input class="form-control" type="file" name="Resume"> </div> <div class="input-group"> <label for="LinkedIn Profile">LinkedIn Profile<span>*</span></label> <input type="url" name="LinkedIn Profile"> </div> <div class="input-group"> <label for="Website link">Website Link <span>*</span></label> <input type="url" name="Website link"> </div> <div class="input-group"> <label for="In lieu of a cover letter, please tell us more about why you are interested in joining the Compass Fellowship?"> In lieu of a cover letter, please tell us more about why you are interested in joining the Compass Fellowship?<span>*</span></label> <input type="text" name="In lieu of a cover letter, please tell us more about why you are interested in joining the Compass Fellowship?"> </div> </div> <button class="submit" type="submit">Apply Now</button> </form> </div> </div>

Here is the structure of how the payload object should look like in the end:以下是payload对象最终应该是什么样子的结构:

let payload = {
  "fields": [
    { "key" : "candidate_first_name", "value" : "John"},
    { "key" : "candidate_last_name", "value" : "Doe"},
    { "key" : "candidate_email", "value" : "john.doe@gmail.com"},
    { "key" : "candidate_phone", "value" : "1234567890"},
    { "key" : "resume", "value": {
      "encoded_data" : "aGVsbG8gd29ybGQ=",
      "file_name" : "resume.txt"
      }
    }
  ]
};

Note the payload structure is from this link , specifically the "Apply for a Opening" section.请注意,有效负载结构来自此链接,特别是“申请职位空缺”部分。

You are running all the code at once ( document.ready() ), instead you need to run it inside form submit , like $('.applicantForm').on('submit', function(e){}) .您一次运行所有代码( document.ready() ),而您需要在form submit运行它,例如$('.applicantForm').on('submit', function(e){}) Check the updated fiddle检查更新的小提琴

 var $ = jQuery; $(document).ready(function() { const ApplyOpeningPayloadBuilder = function() { let payload = { "fields": [] }; return { withKeyValue: function(key, value) { let param = {}; param.key = key; param.value = value; payload.fields.push(param); return this; }, withFile: function(key, encoded_data, filename) { let value = {}; value.encoded_data = encoded_data; value.file_name = filename; this.withKeyValue(key, value); return this; }, build: function() { return payload; } } } let encoded_file = "aGVsbG8gd29ybGQ="; $('.applicantForm').on('submit', function(e) { e.preventDefault(); let apply_for_an_opening_payload_builder = new ApplyOpeningPayloadBuilder(); $(".applicantForm input[type=text]").each(function() { apply_for_an_opening_payload_builder.withKeyValue(this.name, this.value); }); $(".applicantForm input[type=email]").each(function() { apply_for_an_opening_payload_builder.withKeyValue(this.name, this.value); }); $(".applicantForm input[type=tel]").each(function() { apply_for_an_opening_payload_builder.withKeyValue(this.name, this.value); }); $(".applicantForm input[type=url]").each(function() { apply_for_an_opening_payload_builder.withKeyValue(this.name, this.value); }); apply_for_an_opening_payload_builder.withFile("resume", encoded_file, "resume.txt"); let payload = apply_for_an_opening_payload_builder.build(); console.log("Log payload:", payload); }) });
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script> <div class="container" id="json-response"> <div class="form-container"> <div class="header"> <h1>Application form</h1> </div> <form action="#" class="applicantForm"> <div class="form-group"> <div class="input-group"> <label for="First Name">First Name <span>*</span></label> <input type="text" name="First Name"> </div> <div class="input-group"> <label for="Last Name">Last Name <span>*</span></label> <input type="text" name="Last Name"> </div> </div> <div class="form-group"> <div class="input-group"> <label for="Email">Email <span>*</span></label> <input type="email" name="Email"> </div> <div class="input-group"> <label for="Phone">Phone <span>*</span></label> <input type="tel" name="Phone"> </div> </div> <div class="form-group"> <div class="input-group"> <label for="Resume">Resume <span>*</span></label> <input class="form-control" type="file" name="Resume"> </div> <div class="input-group"> <label for="LinkedIn Profile">LinkedIn Profile<span>*</span></label> <input type="url" name="LinkedIn Profile"> </div> <div class="input-group"> <label for="Website link">Website Link <span>*</span></label> <input type="url" name="Website link"> </div> <div class="input-group"> <label for="In lieu of a cover letter, please tell us more about why you are interested in joining the Compass Fellowship?"> In lieu of a cover letter, please tell us more about why you are interested in joining the Compass Fellowship?<span>*</span></label> <input type="text" name="In lieu of a cover letter, please tell us more about why you are interested in joining the Compass Fellowship?"> </div> </div> <button class="submit" type="submit">Apply Now</button> </form> </div> </div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从输入的html表单到javascript对象收集所有值 - Collect all value from input html form to object of javascript 如何从 HTML 表格中收集数据 - How to collect data from HTML form jQuery如何从对象收集所有链接值? - jquery how to collect all link value from a object? 如何从文件夹中收集所有图像 - How to collect all the images from a folder 如何从名称值中收集所有数据以便我可以使用它? - How can i collect all the data from the name value so i can use it? 如何从具有多个键的 json 对象收集数据并将所有值推送到单个键:值数组 - how to collect data from json objects with multiple keys and push all values to single key:value array 如何在变量中收集HTML页面的所有脚本标记 - How to collect all script tags of HTML page in a variable 您如何使用表单从本地 HTML 文件中收集数据并将其存储在本地(这样就无需担心服务器)? - How do you collect data from a local HTML file with a form and store it locally(so that there is no need to worry about servers)? 如何在JavaScript中收集价值? - How to collect value in javascript? 如何使用 javascript 从表单中收集数据并将其存储在变量中? - How can i collect data from form with javascript and store it in variables?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM