當(dāng)前位置:
首頁 >
前端技术
> javascript
>内容正文
javascript
11月8日下午Jquery取属性值(复选框、下拉列表、单选按钮)、做全选按钮、JSON存储、去空格...
生活随笔
收集整理的這篇文章主要介紹了
11月8日下午Jquery取属性值(复选框、下拉列表、单选按钮)、做全选按钮、JSON存储、去空格...
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
1.jquery取復(fù)選框的值
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>無標(biāo)題文檔</title> <!--引入jquery包--> <script src="../jquery-1.11.2.min.js"></script><!--引入的jquery一定是在最上面的--> <style type="text/css"> </style> </head> <body> <input type="checkbox" value="01" class="ck" /> <input type="checkbox" value="02" class="ck" /> <input type="checkbox" value="03" class="ck" /> <input type="checkbox" value="04" class="ck" /> <input type="checkbox" value="05" class="ck" /> <input type="button" value="取選中" id="btn" /> <script type="text/jscript"> //取復(fù)選框的選中值 $("#btn").click(function(){var ck = $(".ck");for(var i=0;i<ck.length;i++){//判斷選中/*if(ck[i].checked){alert(ck[i].value);//js方法}*/if(ck.eq(i).prop("checked"))//prop判斷是否選中 {alert(ck.eq(i).val());}}}) </script> </body> </html>2.取下拉列表里面的屬性值
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>無標(biāo)題文檔</title> <!--引入jquery包--> <script src="../jquery-1.11.2.min.js"></script><!--引入的jquery一定是在最上面的--> <style type="text/css"> </style> </head> <body> <select id="sel"><option value="1111">1111</option><option value="2222">2222</option><option value="3333">3333</option> </select> <input type="button" value="取下拉" id="b1" /> <script type="text/jscript">$("#b1").click(function(){alert($("#sel").val());}) </script> </body> </html>3.取單選鈕的屬性值
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>無標(biāo)題文檔</title> <!--引入jquery包--> <script src="../jquery-1.11.2.min.js"></script><!--引入的jquery一定是在最上面的--> <style type="text/css"> </style> </head> <body> <input class="ck" type="radio" value="01" class="rd" name="a" /> <input class="ck" type="radio" value="02" class="rd" name="a" /> <input class="ck" type="radio" value="03" class="rd" name="a" /> <input class="ck" type="radio" value="04" class="rd" name="a" /> <input class="ck" type="radio" value="05" class="rd" name="a" /><input type="button" value="取單選" id="b2" /> <script type="text/jscript">$("#b2").click(function(){var ck = $(".ck");for(var i=0;i<ck.length;i++){if(ck.eq(i).prop("checked"))//prop判斷是否選中 {alert(ck.eq(i).val());}}}) </script> </body> </html>4.jquery做全選按鈕
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>無標(biāo)題文檔</title> <!--引入jquery包--> <script src="../jquery-1.11.2.min.js"></script><!--引入的jquery一定是在最上面的--> <style type="text/css"> </style> </head> <body> <input type="checkbox" id="qx" />全選 <input type="checkbox" value="01" class="ck" /> <input type="checkbox" value="02" class="ck" /> <input type="checkbox" value="03" class="ck" /> <input type="checkbox" value="04" class="ck" /> <input type="checkbox" value="05" class="ck" /> <script type="text/jscript">$("#qx").click(function(){/*if($(this)[0].checked){$(".ck").attr("checked","checked")}else{$(".ck").removeAttr("checked");}*///標(biāo)記的這段代碼中存在bug,不能用來做全選,要記住。用下面2行代碼。var xz = $(this).prop("checked")//xz接收的值是true(選中)或者flase(未選中)$(".ck").prop("checked",xz)//如果是選中,就是true })</script> </body> </html>5.js或jquery里面有數(shù)據(jù)存儲方式
存儲數(shù)據(jù)的名字叫做JSON
var json = {code:"n001",name:"張三",js:{c:"p001",n:"回族"}};//定義方式。定義比較隨便,可以往里面放任意數(shù)據(jù)。//取值//alert(json.code)//取值方式//alert(json.js.n)//alert(json["name"])6.去空格
var str= " hello "; str = str.trim();//不加取空格輸出的長度是10,包含空格的長度。去掉空格輸出的長度為5. alert(str.length)?
轉(zhuǎn)載于:https://www.cnblogs.com/xiaofox0018/p/6049669.html
總結(jié)
以上是生活随笔為你收集整理的11月8日下午Jquery取属性值(复选框、下拉列表、单选按钮)、做全选按钮、JSON存储、去空格...的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: JQuery radio(单选按钮)操作
- 下一篇: SQL中PIVOT 行列转换