• 表单序列化 P436


    <!DOCTYPE html>
    <html>
    <head>
        <title>表单序列化</title>
        <script type="text/javascript" src="EventUtil.js"></script>
    </head>
    <body>
        <form method="post" action="javascript:alert('Form submitted!')" id="myForm">
            <ul>
                <li><input type="radio" name="color" value="red">Red</li>
                <li><input type="radio" name="color" value="green">Green</li>
                <li><input type="radio" name="color" value="blue">Blue</li>
            </ul>
            <fieldset>
                <legend>Locations</legend>
                <div>
                    <label for="selLocation">Where do you want to live?</label>
                    <select name="location" id="selLocation">
                        <option value="Sunnyvale, CA">Sunnyvale</option>
                        <option value="Los Angeles, CA">Los Angeles</option>
                        <option value="Mountain View, CA">Mountain View</option>
                        <option value="">China</option>
                        <option>Australia</option>
                    </select>
                </div>
                <div>
                    <label for="selLocation2">Where do you want to work?</label>
                    <select name="location2" id="selLocation2" multiple size="5">
                        <option value="Sunnyvale, CA">Sunnyvale</option>
                        <option value="Los Angeles, CA">Los Angeles</option>
                        <option value="Mountain View, CA">Mountain View</option>
                        <option value="">China</option>
                        <option>Australia</option>
                    </select>
                </div>
            </fieldset>
            <div>
                <label for="comments">Any other comments?</label><br>
                <textarea rows="10" cols="50" id="comments" name="comments"></textarea>
            </div>
            <input type="button" value="Serialize Form" id="serialize-btn">
        </form>     
        <script type="text/javascript">
            function serialize(form){        
                var parts = [],
                    field = null,
                    i,
                    len,
                    j,
                    optLen,
                    option,
                    optValue;
                
                for (i=0, len=form.elements.length; i < len; i++){
                    field = form.elements[i];
                
                    switch(field.type){
                        case "select-one":
                        case "select-multiple":
                        
                            if (field.name.length){
                                for (j=0, optLen = field.options.length; j < optLen; j++){
                                    option = field.options[j];
                                    if (option.selected){
                                        optValue = "";
                                        if (option.hasAttribute){
                                            optValue = (option.hasAttribute("value") ? option.value : option.text);
                                        } else {
                                            optValue = (option.attributes["value"].specified ? option.value : option.text);
                                        }
                                        parts.push(encodeURIComponent(field.name) + "=" + encodeURIComponent(optValue));
                                    }
                                }
                            }
                            break;
                            
                        case undefined:     //fieldset
                        case "file":        //file input
                        case "submit":      //submit button
                        case "reset":       //reset button
                        case "button":      //custom button
                            break;
                            
                        case "radio":       //radio button
                        case "checkbox":    //checkbox
                            if (!field.checked){
                                break;
                            }
                            /* falls through */
                                        
                        default:
                            //don't include form fields without names
                            if (field.name.length){
                                parts.push(encodeURIComponent(field.name) + "=" + encodeURIComponent(field.value));
                            }
                    }
                }        
                return parts.join("&");
            }
            
            var btn = document.getElementById("serialize-btn");
            EventUtil.addHandler(btn, "click", function(event){
                var form = document.forms[0];
                alert(serialize(form));
            });
    
        </script>
    </body>
    </html>
  • 相关阅读:
    如何通过js在子页面调用父页面元素的click事件
    时间戳转换成时间格式
    sql 生成某个范围内的随机数
    Ioc思想
    【Sharepoint控件】MOSS模式化窗口
    【Sharepoint代码段】MOSS模拟超级管理员的方法
    【Sharepoint对象模型】MOSS根据模板添加子网站
    【Sharepoint控件】MOSS列表添加时SPUser字段赋值
    【C#代码段】asp在数据库加入数据
    【C#代码段】ajax从asp后台获取数据
  • 原文地址:https://www.cnblogs.com/jiunie/p/13549624.html
Copyright © 2020-2023  润新知