jquery Selector Quick Extract Web Form data sample

Source: Internet
Author: User
Tags json trim

  When you have to repeat the same thing many times, you want to find a time-saving method, and here's a quick way to extract Web form data using the jquery selector

I am lazy, when doing the web, when encountered to repeat the same thing many times, want to find a time-saving method.     Prior to submitting the form in the web and need to validate user input, when extracting user input information, it is necessary to use document.getElementById () in JS, so that a two-form field is OK, But there are a lot of times (I have encountered more than 10 of cases) to see all the dizziness, so the individual is more disgusted with this way, but fortunately, I myself blindly pondering, using jquery found a convenient way.     I add an additional attribute to each form field that needs to submit the data, replacing the original name property with its own additional defined attribute "_postfield", such as <input type= "text" _postfield= "name"/ ><input type= "Radio" _postfield= "sex"/> and so on, and then use query to get all the DOM objects that have _postfield properties and traverse them, encapsulated in the value of _postfield as key, Its content is value of the JSON key values pair, its JS method is as follows:    code as follows: <script type= "text/javascript>  function Getformfield (SEL) {  var Objs = $ ("*[" +sel+ "]");  var postdata = {};  for (var i=0,len=objs.length;i<len;i++) {  var ob j = objs[i];  var nodename = obj.nodeName.toLowerCase ();  var field = $ (obj). attr (SEL);    if (nodename = = "Input") {  if (Obj.type.trim () = "Radio" && (obj.checked| | obj.checked== "Checked")) {    Postdata[field] = $ (obj). val ();  continue; }  if (Obj.type.trim () = = "checkbox" && (obj.checked| | obj.checked== "Checked") {  var ov = postdata[field]| | "";   var NV = ov+ "," +$ (obj). val ();  Postdata[field] = Nv.replace (/^,+/, "");  continue; }    I F (obj.type.trim () = = "Text" | | Obj.type.trim () = = "hidden") {  Postdata[field] = $ (obj). val ();  continue; }    continue;  }    if (nodename== "textarea") {  Postdata[field] = $ (obj). val ();  continue; }  & nbsp if (nodename== "select") {  var val = obj.options[obj.selectedindex].value;  Postdata[field] = val;  continue; }    Postdata[field] = $ (obj). html ();   }   //return JSON data to get data from form   return postdata; }  </script>    test Code:  code is as follows: <form style= "margin-left:200px; margin-top:300px; " >  <input type= "text" value= "_postfield=" name "/><br/><br/>  <input type=" Radio " Value= "male" name= "sex" _postfield= "sex"/> male   <input tyPe= "Radio" value= "name=" Sex "_postfield=" sex "/> Women   <input type=" Radio "value=" neutral "name=" sex "_postfield=" Sex "/> Neutral <br/><br/>  <select _postfield=" Job ">  <option value=" Work "> Work </ option>  <option value= "begging" > Begging </option>  <option value= "Nothing is irrelevant" > nothing. </option>   </select>  <br/><br/>  <input type= "button" value= "OK" onclick= "test ();"/>& nbsp </form>  <script>  function Test () {  var postdata = Getformfield ("_postfield");    var sb = [];  for (var o in postdata) {  sb.push (o+ "=" +postdata[o]); }  alert (Sb.join ("n")); &nb Sp }  </script>   

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.