function getFormData(){var elements = document.getElementById("getFormData").elements;// all form elementsvar fields =Object.keys(elements).map(function(k){if(elements[k].name !==undefined){return elements[k].name;// special case for Edge's html collection}elseif(elements[k].length >0){return elements[k].item(0).name;}}).filter(function(item, pos, self){return self.indexOf(item)== pos && item;});var data ={};
fields.forEach(function(k){
data[k]= elements[k].value;var str ="";if(elements[k].type =="checkbox"){
str = str + elements[k].checked +", ";
data[k]= str.slice(0,-2);// remove the last comma and space from the string to make the output prettier in the spreadsheet}elseif(elements[k].length){for(var i =0; i < elements[k].length; i++){if(elements[k].item(i).checked){
str = str + elements[k].item(i).value +", ";// same as above
data[k]= str.slice(0,-2);}}}});return data;}/**
* When the form is submitted
*/function handleFormSubmit(event){
event.preventDefault();var data = getFormData();
console.log(data);// Must be valid. Lets send the datavar url = event.target.action;var xhr =newXMLHttpRequest();
xhr.open('POST', url);
xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
xhr.onreadystatechange =function(){// Once submitted hide the form
dcument.getElementById('form').style.display ='none';// hide the form
document.getElementById('thankyou').style.display ='block';return;};// url encode form data for sending as post datavar encoded =Object.keys(data).map(function(k){return encodeURIComponent(k)+'='+ encodeURIComponent(data[k])}).join('&')
xhr.send(encoded);}/**
* On view load
*/function loaded(){// On form submitvar form = document.getElementById('gform');
form.addEventListener("submit", handleFormSubmit,false);// Thank you return back to form
document.getElementById('back-button').addEventListener("click",function(){
document.getElementById('form').style.display ='block';
document.getElementById('thankyou').style.display ='none';// hide the thank you});}
document.addEventListener('DOMContentLoaded', loaded,false);
السؤال
Mona Hegazy2
يسمح باستقبال البيانات ما عدا checkboox
ما السبب
1 جواب على هذا السؤال
Recommended Posts
انضم إلى النقاش
يمكنك أن تنشر الآن وتسجل لاحقًا. إذا كان لديك حساب، فسجل الدخول الآن لتنشر باسم حسابك.