jQuery序列化表单为JSON对象

nf456的头像 nf456 6 2015-01-04 22:08 0

 基本信息

× 1    × 1   

浏览数: 4346

分享时间: 3 年 前

2
    <form id="myform">  
        <table>  
            <tr>  
                <td>姓名:</td>  
                <td> <input type="text" name="name" /> </td>  
            </tr>  
            <tr>  
                <td>性别:</td>  
                <td>  
                    <input type="radio" name="sex" value="1"> 男  
                    <input type="radio" name="sex" value="0"> 女  
                </td>  
            </tr>  
            <tr>  
                <td>年龄:</td>  
                <td>  
                    <select name="age">  
                        <option value="20">20</option>  
                        <option value="21">21</option>  
                        <option value="22">22</option>  
                    </select>  
                </td>  
            </tr>  
            <tr>  
                <td>爱好</td>  
                <td>  
                    <input type="checkbox" value="basketball" name="hobby">篮球  
                    <input type="checkbox" value="volleyball" name="hobby">排球  
                    <input type="checkbox" value="football" name="hobby">足球  
                    <input type="checkbox" value="earth" name="hobby">地球  
                </td>  
            </tr>  
            <tr>  
                <td colspan="2">  
                    <input type="button" id="ajaxBtn" value="提交" />  
                </td>  
            </tr>  
        </table>  
    </form>  

    <script type="text/javascript">  
          
        $(function() {  
            $("#ajaxBtn").click(function() {  
                    var params = $("#myform").serializeObject(); //将表单序列化为JSON对象   
                    console.info(params);  
                })  
        })  
      
        $.fn.serializeObject = function() {  
            var o = {};  
            var a = this.serializeArray();  
            $.each(a, function() {  
                if (o[this.name]) {  
                    if (!o[this.name].push) {  
                        o[this.name] = [ o[this.name] ];  
                    }  
                    o[this.name].push(this.value || '');  
                } else {  
                    o[this.name] = this.value || '';  
                }  
            });  
            return o;  
        }  
    </script>  


12 [下一页]

  • annashetty的头像 annashetty 2018-11-06 17:36 代码数:0

    I am grateful to have opened this discussion. This question is quite interesting to me. Finally the answer was found driving directions

  • ossaa的头像 ossaa 2018-11-11 18:16 代码数:0

    The information you have posted is very useful. The sites you have referred was good. Thanks for sharing...

    digital marketing

  • ossaa的头像 ossaa 2018-11-12 23:25 代码数:0

    I’ve been searching for some decent stuff on the subject and haven't had any luck up until this point, You just got a new biggest fan!..

    shakadang trail

  • ossaa的头像 ossaa 2018-05-19 20:50 代码数:0

    I can see that you are an expert at your field! I am launching a website soon, and your information will be very useful for me.. Thanks for all your help and wishing you all the success in your business.  selfishmum.co.uk

  • ossaa的头像 ossaa 2018-05-24 18:28 代码数:0

    I really appreciate this wonderful post that you have provided for us. I assure this would be beneficial for most of the people.

    Read More

  • ossaa的头像 ossaa 2018-05-25 00:53 代码数:0

    I can see that you are an expert at your field! I am launching a website soon, and your information will be very useful for me.. Thanks for all your help and wishing you all the success in your business.Website nhap hang Mona Media

  • ossaa的头像 ossaa 2018-05-26 18:31 代码数:0

    This is such a great resource that you are providing and you give it away for free. I love seeing websites that understand the value of providing a quality resource for free. It is the old what goes around comes around routine.

    service aer condiționat

  • ossaa的头像 ossaa 2018-05-27 23:54 代码数:0

    I'm glad I found this web site, I couldn't find any knowledge on this matter prior to.Also operate a site and if you are ever interested in doing some visitor writing for me if possible feel free to let me know, i am always look for people to check out my web site.

    Top ICO List

  • ossaa的头像 ossaa 2018-05-29 16:49 代码数:0

    i read a lot of stuff and i found that the way of writing to clearifing that exactly want to say was very good so i am impressed and ilike to come again in future.. 

    web Design Lincoln NE

  • ossaa的头像 ossaa 2018-05-29 22:01 代码数:0

    Excellent information on your blog, thank you for taking the time to share with us. Amazing insight you have on this, it's nice to find a website that details so much information about different artists.

    Jewelry Consignment Shop Dallas

您的评论: