js獲取form表單所有數據


 JS批量獲取form表單所有數據

1.有的時候想偷點懶,頁面上有大量的表單提交數據,每次單獨獲取都比較麻煩。代碼冗余度也比較多,因此封裝了一個方法。

可通過設置相同發class名稱,使用此封裝方法,批量獲取form表單里面的值

2. 表單元素必須要有name屬性,name屬性是向后端提交的字段數據。

3.html代碼

<h3>下拉框</h3>
    <select name="sel" id="sel" class="query">
        <option value ="sel-1">sel-1</option>
        <option value ="sel-2">sel-2</option>
    </select>
    <h3>輸入框</h3>
    <input type="text" name="text1"  class="query" value="hello" />
    <input type="text" name="text2"  class="query" value="word" />
    <h3>密碼框</h3>
    <input type="password" name="password" class="query" value="123456" />
    <h3>單選框</h3>
    單選1<input type="radio" name="radio" class="query" value="r1" checked />
    單選2<input type="radio" name="radio" class="query" value="r2"  checked/>
    單選3<input type="radio" name="radio" class="query" value="r3" />
     <h3>復選框</h3>
    復選框1<input type="checkbox" name="check" id="" class="query" value="c1" checked/>
    復選框2<input type="checkbox" name="check" id="" class="query" value="c2"  />
    復選框3<input type="checkbox" name="check" id="" class="query" value="c3"  checked/>
    <h3>search</h3>
    <input type="range" name="range" id="" class="query" value="" />
    <input type="color" name="color" id="" class="query" value="" />
    <h3>
        <button type="button" id="save">
            提交
        </button>
    </h3>

4.此處引入了JQ庫。

4.1js代碼塊

使用說明:調用方法的時候傳入class名稱即可。

// 封裝方法,獲取到form表單的數據。使用此方法,表單元素必須存在name屬性。
        //el:元素的class名稱。
        function getParameter(el){
            var obj={};
            $(el).each(function(index,item){
                // 判斷元素的類型
                if(item.type=="text" || item.type=="password" || item.type=="select-one"  || item.type=="tel"  || 
                    item.type=="search" || item.type=="range" || item.type=="number" || item.type=="month" || 
                    item.type=="email" || item.type=="datetime-local" || item.type=="datetime" || item.type=="date" || 
                    item.type=="color"){
                    //獲取到name的值,name的值就是向后台傳遞的數據
                    obj[$(this).attr("name")]=$(this).val();
                }else if(item.type=="checkbox"){
                    var stamp=true;
                    if($(this).attr("name") && !stamp){
                        stamp=false;
                        // 獲取到復選框選中的元素
                        var checkboxEl=$("input[name="+$(item).attr('name')+"]:checked");
                        if(checkboxEl){
                            var checkboxArr=[];
                            // 取出復選框選中的值
                            checkboxEl.each(function(idx,itm){
                                checkboxArr.push($(itm).val());
                            });
                            obj[$(this).attr("name")]=checkboxArr.join(",");
                        }
                        
                    }
                }else if(item.type=="radio"){
                    // 獲取到單選框選中的值
                    var radio_val=$("input[name="+$(item).attr('name')+"]:checked").val();
                    if(radio_val){
                        obj[$(item).attr("name")]=radio_val;
                    }
                }
            });
            return obj;
        }
        // 調用方法
        $("#save").click(function(){
           var parameter=getParameter(".query");
       console.log(parameter);
     });
      

原文鏈接地址:https://mp.weixin.qq.com/s/61VyfaYgPK-IjDq2m2-HKg



免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM