• 【JS】form相关


    Form的序列化

    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("&");
    }

    对select的操作建议方法:
    1、获取option的value的text:selectElement.options[index].text/selectElement.options[index].value

    2、设置某选项被选择:selectElement.options[index].selected = true;   //在单选的情况下会取消其他项的选择

    3、添加option的最佳方法:

    //最佳方案
    var newOption = new Option("text","value");
    selectElement.add(newOption,undefined);
    
    //效率相对较低,代码编写量大
    var newOption = document.createElement("option");
    newOption.appendChild(document.createTextNode(textTextbox.value));
    newOption.setAttribute("value", valueTextbox.value);
    selectElement.appendChild(newOption);
    
    //IE8之前有问题
    var newOption = new Option("text","value");
    selectElement.appendChild(newOption);

    4、移除选项:selectElement.remove(index) 或者 selectElement.removeChild(selectElement.options[index])

    5、向select任意位置插入项:selectElement.insertBefore(baseOption,newOption);

  • 相关阅读:
    基于 HTML5 WebGL 的发动机 3D 可视化系统
    基于 HTML + WebGL 结合 23D 的疫情地图实时大屏 PC 版
    用 HTML5 造个有诚意的 23D 招聘稿
    基于 HTML5 Canvas 的 3D 热力云图效果
    基于 HTML5 和 Canvas 实现的 3D 垃圾分类系统
    xcode10 改动
    __NSArrayI __NSArray0 __NSSingleObjectArrayI __NSPlaceholderArray __NSArrayM
    iOS开发之Found a swap file by the name ".podfile.swp" owned by: Netban dated:...file name: ~N...
    关于程序的测试
    ios the request was denied by service delegate for reason unspecified
  • 原文地址:https://www.cnblogs.com/yangzhilong/p/3092550.html
Copyright © 2020-2023  润新知