自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

jQuery中RadioButtonList的功能及用法

開(kāi)發(fā) 開(kāi)發(fā)工具
這里將介紹jQuery中RadioButtonList的功能及用法,希望本文能對(duì)大家了解RadioButtonList有所幫助。

對(duì)于RadioButtonList的功能及用法,本文將從程序規(guī)則開(kāi)始介紹,在后面的實(shí)例中還涉及C#生成HTML,設(shè)置Config: AppSettings等等內(nèi)容。

首先介紹程序規(guī)則:

1.對(duì)以下的選擇進(jìn)行檢查,檢查是否全部選中

2,所謂的“全部選中”是指,一行中三個(gè)radiobutton必須有一個(gè)radiobutton被選中。

3. 這里一共有33個(gè)radiobutton,每三個(gè)為一組;

以下界面里面的所有元素都是從后臺(tái)生成的(當(dāng)然也可以前臺(tái)生成),后面我會(huì)把生成的代碼也附上。僅參考:)

后臺(tái)生成

首先看一下HTML代碼結(jié)構(gòu):

Html代碼結(jié)構(gòu)

一開(kāi)始我為了進(jìn)行(選擇、未選擇)Check之前,必須先找到相應(yīng)的對(duì)象。

從Html Render的結(jié)果來(lái)看,每個(gè)RadiobuttonList生成出來(lái)的Html代碼:

都是一個(gè)table,此table有三列,每列有一個(gè)type為radio的radioButton.

radiobuttonList生成出來(lái)的Html代碼

 

這樣的話,有整個(gè)生成出來(lái)的結(jié)果,就會(huì)有11個(gè)table,每個(gè)table有3個(gè)radiobutton,一共33個(gè)radiobutton.

那我們想找到table對(duì)象為ctl14,這就可以用到Jquery強(qiáng)大選擇器進(jìn)行相應(yīng)查找工作:

$("#Table1").find($("table[id=ctl14]")) 好了,我現(xiàn)在可以找到***個(gè)對(duì)象了,那其他table對(duì)象也就可以容易找到了。

這樣,注意的每個(gè)生成table的對(duì)象id都是以ctl開(kāi)頭的,這樣的話,查找每個(gè)table的JQ – Code:

$("#Table1").find($("table[id^=ctl]")) 好了,現(xiàn)在我們已經(jīng)找到了每個(gè)table一級(jí)了;

接下來(lái),我只要在找每個(gè)table中radiobutton對(duì)象就可以進(jìn)行(選擇、未選擇)Check了.

代碼為:

$("table[id^=ctl] input[type=radio]") 或者   $("table[id^=ctl]").find("input[type=radio]")

到這里我們所有要的對(duì)象都找到。是不是很輕松。

代碼分析:

  1. $("#<%= ibSubmit.ClientID%>").click(function() {   
  2.         var flag = true;   
  3.         //alert($("table[id^=ctl]").length);  
  4.         //創(chuàng)建一個(gè)checked的arr數(shù)組,用于存儲(chǔ)每個(gè)radiobutton的checked情況   
  5.          var arr = new Array;   
  6.         $("table[id^=ctl] input[type=radio]").each(function(i) {   
  7.                arr.push(this.checked);   
  8.         });  
  9.  
  10.         //然后再創(chuàng)建一個(gè)arrTrue數(shù)組,用于過(guò)濾false的arr數(shù)組  
  11.  
  12.          var arrTrue = new Array;   
  13.          $.each(arr, function(i) {   
  14.              if (arr[i] == true) {   
  15.                  arrTrue.push(arr[i]);   
  16.              }   
  17.          });  
  18.  
  19.          //當(dāng)然也可以用grep函數(shù),來(lái)簡(jiǎn)化過(guò)濾arr數(shù)組操作  
  20.  
  21.           //arr = $.grep(arr, function(n, i) {   
  22.          //return n == true;   
  23.          //});  
  24.  
  25.  
  26.          var groupLen = Math.floor($("table[id^=ctl]").length + 1 / 3);  
  27.  
  28.          //***,簡(jiǎn)單一點(diǎn)吧,只有判斷arrTrue的長(zhǎng)度是否為11,就可以。  
  29.  
  30.          //因?yàn)槭且还?3個(gè)radiobutton,每3個(gè)為一組,規(guī)則又是一組中3選1,        //所以全部選擇肯定有11個(gè)radiobutton被選中。   
  31.           if (arrTrue.length != groupLen) {   
  32.                 flag = false;   
  33.          }   
  34.      return false;  
  35.  
  36. }); 

完整代碼:

  1. $(function() {   
  2.             $("#<%= ibSubmit.ClientID%>").click(function() {   
  3.                 var flag = true;   
  4.                 var arr = new Array;   
  5.                 $("table[id^=ctl] input[type=radio]").each(function(i) {   
  6.                     arr.push(this.checked);   
  7.                 });   
  8.                 arr = $.grep(arr, function(n) {   
  9.                     return n == true;   
  10.                 });   
  11.                 var groupLen = Math.floor($("table[id^=ctl]").length + 1 / 3);   
  12.                 if (arr.length != groupLen) {   
  13.                     flag = false;   
  14.                 }   
  15.                 return false;   
  16.             });   
  17.         }); 

***附上生成Table代碼:(兩種生成方法,JQuery版本,C#版本) – (可看可不看)

jQuery版本

  1. var array = ["XXXX",""XXXX","XXX"];  
  2. $.each(array, function(i) {  
  3.          table.append("<tr><td>"+  
  4.                       "<input id='cbl_" + i + "'" + " type='checkbox' title='" + array[0] + "' />" +  
  5.                       "</td></tr>");  
  6. });  

C#版本

  1. private void DynamicCreateTable () {   
  2. NameValueCollection titleList = ConfigurationManager.GetSection ( sectionName ) as NameValueCollection;   
  3. {  for ( int i = 0 ; i < titleList.Count ; i++ )   
  4. { HtmlTableRow tr = new HtmlTableRow ();    
  5. {  tr.Cells.Add ( BuilderTableCell ( titleList , i ) );    
  6. tr.Cells.Add ( BuilderTableCellWithRadio ( i ) );    
  7. if ( i % 2 == 0 ) { tr.Style.Add ( "background-color" , "#ffc" );   
  8. }   
  9.  }   
  10.  tbTraDemand.Rows.Add ( tr );   
  11.               }       
  12.       }     
  13.     }        
  14.    private HtmlTableCell BuilderTableCellWithRadio ( int i )   
  15. {   HtmlTableCell tc = new HtmlTableCell ();    
  16. {  tc.ColSpan = 3;     
  17.  RadioButtonList rbl = new RadioButtonList ();    
  18.  {   rbl.Items.Add ( new ListItem ( "" , "1" ) );      
  19.  rbl.Items.Add ( new ListItem ( "" , "2" ) );    
  20. rbl.Items.Add ( new ListItem ( "" , "3" ) );    
  21.                  rbl.Style.Add ( "width" , "100%" );      
  22.                rbl.RepeatDirection = RepeatDirection.Horizontal;       
  23.           }            
  24.      tc.Controls.Add ( rbl );      
  25.        }             
  26. return tc;       }           
  27. private HtmlTableCell BuilderTableCell ( NameValueCollection titleList , int i )   
  28. { HtmlTableCell tc = new HtmlTableCell ();    
  29.          {               tc.Style.Add ( "width" , "40%" );     
  30.             tc.Align = "left";           
  31.       Label lbl = new Label ();          
  32. lbl.Text = String.Concat ( "&nbsp;&nbsp;" , titleList.AllKeys[i] );               
  33. tc.Controls.Add ( lbl );          
  34.    }           return tc;         
  35. }  

Config: AppSettings 

  1. <configSections>     
  2.          <sectionGroup name="MarketReSearch">     
  3.  <section name="TravelReSearchTitle" type="System.Configuration.NameValueSectionHandler"/>            
  4.  </sectionGroup>     
  5.     </configSections>     
  6.      <MarketReSearch>     
  7.          <TravelReSearchTitle>     
  8.             <add key="XXXX訂" value="1"/>     
  9.             <add key="XXXX訂" value="2"/>             </TravelReSearchTitle>    
  10.      </MarketReSearch> 

原文標(biāo)題:JQuery RadioButtonList

鏈接:http://www.cnblogs.com/RuiLei/archive/2009/09/04/1560129.html

【編輯推薦】

  1. jQuery調(diào)用WCF服務(wù)傳遞JSON對(duì)象
  2. 學(xué)習(xí)jQuery必須知道的幾種常用方法
  3. 用XML+XSLT+CSS+JQuery組建ASP.NET網(wǎng)站
  4. 使用jQuery和PHP構(gòu)建一個(gè)受Ajax驅(qū)動(dòng)的Web頁(yè)面
  5. jQuery調(diào)用WCF需要注意的一些問(wèn)題
責(zé)任編輯:彭凡 來(lái)源: 博客園
相關(guān)推薦

2010-06-30 12:40:47

Linux SNMP

2010-07-05 11:32:54

HART協(xié)議

2013-05-29 09:29:07

OSI傳輸層TCP協(xié)議

2017-10-11 14:45:58

Linuxinotify功能實(shí)現(xiàn)原理

2011-11-24 11:24:45

iCloud云計(jì)算蘋(píng)果

2010-03-18 17:24:31

無(wú)線ZigBee網(wǎng)絡(luò)

2010-07-09 11:37:29

動(dòng)態(tài)路由選擇協(xié)議

2010-06-30 11:17:53

SNMP監(jiān)測(cè)交換機(jī)

2010-06-24 16:47:45

Linux Chgrp

2011-09-14 09:56:48

HTML 5

2019-10-31 10:16:06

數(shù)據(jù)Python開(kāi)發(fā)

2020-10-15 10:10:31

Linux數(shù)據(jù)中心IT

2009-12-17 13:55:52

Visual Stud

2015-11-02 14:09:01

2010-02-07 11:37:53

2009-11-23 09:54:52

2010-01-11 09:46:56

智能交換機(jī)

2009-09-02 17:44:41

C#字符串處理

2010-01-14 17:46:29

智能交換機(jī)

2010-03-16 15:36:32

網(wǎng)絡(luò)交換機(jī)
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)