JS implements the method that receives the value of the form and _javascript the value behind the form action. Technique

Source: Internet
Author: User

This example describes the method that JS implements to receive form values and to spell the values behind the form action. Share to everyone for your reference, specific as follows:

Today encountered a problem, in the form form there are several input, which has a upload file input, now need to click on the Submit button, will not file type of input is worth and spelled &name=value format added to the action behind, So we can get it out together.

<form id= "MyForm" name= "MyForm" "method=" Post "action=" Http://www.yoursiteweb.com:8080/justsy/Dolet?type=ws_ Justsy_webclips_policy_update "Enctype=" Multipart/form-data > <!--background generates JSON--> <script type= "text/ JavaScript > var data = [{label: "Basic Information", type: "title", Tag: "H1"}, {label: "Name", type: "Input", Input: "text", Optional: False,name: "PolicyName", ID: "PolicyName", Value: ""}, {label: "Description", type: "Input", Input: "text", Optional: False,name: "PolicyDescription", ID: "PolicyDescription", Value: ""}, {label: "", type: "title", Tag: "H1"}, {label: "label (Required) ", type:" Input ", Input:" Text ", Optional:true, Name:" Label ", ID:" Label ", Value:" "}, {Label:" URL (Required) ", Type: "Input", Input: "Text", Optional:true, Name: "URL", id: "url", Value: ""}, {label: "Removeable", type: "Input", Input: " CheckBox ", Optional:false,name:" isremovable ", ID:" IsRemovable ", Value:" ", Accept:" image/* "}, {label:" Icon ", type:" Input ", Input:" File ", Optional:true, Name:" Picurl ", ID:" IconFile ", Value:" "}, {label:" PrecomposeD Icon ", type:" Input ", Input:" checkbox ", Optional:false,name:" precomposed ", ID:" precomposed ", Value:" "}, {label:" full Screen ", type:" Input ", Input:" checkbox ", Optional:false,name:" fullscreen ", ID:" fullscreen ", Value:" "}, {type:" hidden ", Name:" PolicyId ", ID:" PolicyId ", Value:" "}, {type:" hidden ", Name:" Payloaddescription ", ID:" Payloaddescription ", Value: ""}, {type: "hidden", Name: "Payloaddisplayname", ID: "Payloaddisplayname", Value: ""}, {type: "hidden", Name: " Payloadidentifier ", ID:" Payloadidentifier ", Value:" "}, {type:" hidden ", Name:" Payloadorganization ", ID:" Payloadorganization ", Value:"}, {type: "hidden", Name: "Payloadtype", ID: "Payloadtype", Value: ""}, {type: "hidden", Name: "Payloaduuid", ID: "Payloaduuid", Value: ""}, {type: "hidden", Name: "Payloadversion", ID: "Payloadversion", Value: "" 
//{type: "Hidden", Name: "icon", ID: "Icon", Value: ""}, {type: "Submit", Optional:true,func: ""}]; Inputs (data)//Call this method at the place where you want to display the input box </script> <div class= "Btn-bar" > <input type= "button" onclick= " SubmitForm () "Value"= "Save" > </div> </form>

 

Main JS Code:

function Getparams () {var Doclist=document.getelementbyid ("Inputlist"). getElementsByTagName ("input");
var str= ""; 
alert (doclist.length); for (Var i=0;i<doclist.length-1;i++) {if (Doclist[i].getattribute ("type") = = "checkbox") {if (doclist[i].checked ) Str+=getparam (Doclist[i].getattribute ("name"), "1") Else Str+=getparam (Doclist[i].getattribute ("name")
, "0")} else Str+=getparam (Doclist[i].getattribute ("name"), Doclist[i].value); 
return str; 
function GetParam (key,value) {return "&" +key+ "=" +value; 
   function SubmitForm () {if (document.getElementById ("PolicyId"). Value = = "") {alert ("the company does not create a policy"); 
  return false; 
 } else{document.getElementById ("Iconfile_error"). Innerhtml= "Submitting ..."; 
  /* Gets the file name of the file upload and determines whether the extension is jpg*/var Testmsg=document.getelementbyid ("IconFile"). Value; 
  var filename=testmsg.replace (/.* (\/|\\)/, ""); var fileext= (/[.] /.exec (filename))? /[^.] 
   +$/.exec (Filename.tolowercase ()): '; Iffileext!= ' jpg ') {document.getElementById ("Iconfile_error"). innerhtml= "";   
   Alert ("Please upload picture"); 
   return false; 
  } document.getElementById ("MyForm"). Action+=getparams (); 
  document.getElementById ("MyForm"). Submit ();

 } 
}

So that we can spell the past .... It's very useful .... You can also check the values in the checkbox ... Checked to 1, not selected to 0

I hope this article will help you with JavaScript programming.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.