利用JS如何獲取form表單數(shù)據(jù)
前言
本文主要給大家介紹的是關(guān)于利用JS獲取form表單數(shù)據(jù)的相關(guān)內(nèi)容,分享出來供大家參考學(xué)習(xí),下面話不多說了,來一起看看詳細(xì)的介紹吧
方法如下:
1.有的時候想偷點懶,頁面上有大量的表單提交數(shù)據(jù),每次單獨獲取都比較麻煩。代碼冗余度也比較多,因此封裝了一個方法。
2. 表單元素必須要有name屬性,name屬性是向后端提交的字段數(shù)據(jù)。
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>復(fù)選框</h3> 復(fù)選框1<input type="checkbox" name="check" id="" class="query" value="c1" checked/> 復(fù)選框2<input type="checkbox" name="check" id="" class="query" value="c2" /> 復(fù)選框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代碼塊
使用說明:調(diào)用方法的時候傳入class名稱即可。
// 封裝方法,獲取到form表單的數(shù)據(jù)。使用此方法,表單元素必須存在那么屬性。 //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的值就是向后臺傳遞的數(shù)據(jù) obj[$(this).attr("name")]=$(this).val(); }else if(item.type=="checkbox"){ var stamp=false; if($(this).attr("name") && !stamp){ stamp=false; // 獲取到復(fù)選框選中的元素 var checkboxEl=$("input[name="+$(item).attr('name')+"]:checked"); if(checkboxEl){ var checkboxArr=[]; // 取出復(fù)選框選中的值 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; } // 調(diào)用方法 $("#save").click(function(){ var parameter=getParameter(".query"); console.log(parameter); });
總結(jié)
以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,謝謝大家對腳本之家的支持。
相關(guān)文章
Javascript頁面跳轉(zhuǎn)常見實現(xiàn)方式匯總
這篇文章主要介紹了Javascript頁面跳轉(zhuǎn)常見實現(xiàn)方式,結(jié)合實例匯總分析了JavaScript常用的七種頁面跳轉(zhuǎn)實現(xiàn)技巧,具有一定參考借鑒價值,需要的朋友可以參考下2015-11-11iframe自適應(yīng)寬度、高度 ie6 7 8,firefox 3.86下測試通過
近期需要一個iframe自適應(yīng)高度的東西,在網(wǎng)上找了很多,都不能用……一看大體的日期都是大概 2008年前后的其他近期的基本都是以前的轉(zhuǎn)載,所以只好自己動手了。2010-07-07JavaScript 開發(fā)中規(guī)范性的一點感想
在開發(fā)中通用的幾個方法,我們把它們放到utility目錄下或者utility.js中;所有的提示信息和報錯信息統(tǒng)一放置在一起??雌饋矶际切⌒〉膸撞剑瑓s能讓咱們開發(fā)的代碼同事讀起來更順暢,下個項目中也能用上。2009-06-06