JQuery对基本Html控件的操作TextBox、Select、Radio、CheckBox .

废话少说,将JQuery对Html控件的基本操作集成到一个Aspx页面,让初学者有个直观的了解。JQuery老鸟可以直接忽略。直接拷贝下来放到本地页面即可直观查看。

 
[javascript] view plain copy print ?
  1. <%@ Page Language="C#" AutoEventWireup="true" CodeFile="ControlGetAndSet.aspx.cs" Inherits="JQueryExample_ControlGetAndSet" %>  
  2.   
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
  4.   
  5. <html xmlns="http://www.w3.org/1999/xhtml">  
  6. <head runat="server">  
  7.     <title></title>  
  8.   
  9.     <mce:script src="../JS/jquery-1.3.2.min.js" mce_src="JS/jquery-1.3.2.min.js" type="text/javascript"></mce:script>  
  10.     <mce:script type="text/javascript"><!--  
  11.         function GetText() {  
  12.             var text = $("input[name=text1]").val();  
  13.             alert(text);  
  14.               
  15.             //用id获取  text的value值   
  16.             var tmp = $("#text1").val();  
  17.             alert(tmp);  
  18.         }  
  19.   
  20.   
  21.         function SetText() {  
  22.             var text = $("input[name='text1']");  
  23.             text.val("用Name获取赋值");  
  24.             //var tmp = $("#text1").val("用ID获取赋值");   
  25.         }  
  26.   
  27.           
  28.         //获取Select选中值   
  29.         function GetSelect() {  
  30.             var sel = $("select[name='select1']").text();  
  31.             //var sel = $("select[name='select1'] option[selected]").text();   
  32.               
  33.             //通过id获取   
  34.             //var sel = $("#select1").text();    
  35.   
  36.             alert(sel);  
  37.         }  
  38.           
  39.         //设置选中项的值  未实现   只能设置已有项选中   
  40.         function SetSelect() {  
  41.             //$("#select1").val("北京");//将value为“北京”的项选中   
  42.             $("#select1 ").get(0).selectedIndex = 1  
  43.         }  
  44.           
  45.         //添加Option   
  46.         function AddSelectOptions() {  
  47.             //$("<option>北京</option>").appendTo("#select1");   
  48.             //for(var i=0;i<5;i++)   
  49.             //{   
  50.             //    $("<option>" + i + "</option>").appendTo("#select1");   
  51.             //}   
  52.   
  53.             //prepend为第一个,append(appendTo)为追加   
  54.             $("#select1").append("<option>北京</option>");  
  55.             for(var i=0;i<5;i++)  
  56.             {  
  57.                 $("#select1").append("<option>" + i + "</option>");  
  58.             }  
  59.               
  60.               
  61.         }  
  62.           
  63.         //移除选中项   
  64.         function RemoveSelectOptions() {  
  65.             $("#select1 option[selected]").remove();  
  66.         }  
  67.           
  68.         //清空option   
  69.         function ClearSelectOptions() {  
  70.             //$("#select1").empty();   
  71.             $("#select1").html("");  
  72.         }  
  73.   
  74.   
  75.   
  76.   
  77.   
  78.         function GetCheck() {  
  79.             var check = $("#checkbox1").attr("checked");  
  80.             alert(check);  
  81.         }  
  82.   
  83.         function SetCheck() {  
  84.             $("#checkbox1").attr("checked"true);  
  85.         }  
  86.         function AddCheck() {  
  87.             $("<input type='checkbox' />").appendTo("#form1");  
  88.         }  
  89.           
  90.           
  91.   
  92.         function GetRadio() {  
  93.             var ra = $("input[type='radio'][checked]").attr("value");  
  94.             //var ra = $("input[type='radio'][checked]").val();   
  95.             alert(ra);  
  96.         }  
  97.   
  98.         function SetRadio() {  
  99.             //$("input[type='radio']").get(0).checked = true;   
  100.             $("input[type='radio'][value=2]").attr("checked","checked");  
  101.         }  
  102.       
  103. // --></mce:script>   
  104.       
  105.       
  106.       
  107.       
  108.    
  109. </head>  
  110. <body>  
  111.     <form id="form1" runat="server">  
  112.     <div>  
  113.         <input id="checkbox1" type="checkbox" value="是否正确"  style="width:100px" />  
  114.         <input id="btnGetCheck" type="button" value="获取Check是否选中" οnclick="GetCheck()" />  
  115.         <input id="btnSetCheck" type="button" value="设置Check选中" οnclick="SetCheck()" />  
  116.         <input id="btnAddCheck" type="button" value="新增Check" οnclick="AddCheck()" />  
  117.           
  118.     </div>  
  119.     <div>  
  120.       
  121.         <input id="radio1" type="radio"  value="1" style="width:50px" />  
  122.         <input id="radio2" type="radio" value="2" style="width:50px" />  
  123.         <input id="btnGetRadio" type="button" value="获取Radio是否选中" οnclick="GetRadio()" />  
  124.         <input id="btnSetRadio" type="button" value="设置Radio选中" οnclick="SetRadio()" />  
  125.     </div>  
  126.       
  127.     <div>  
  128.           
  129.         <select id="select1" name="select1">  
  130.             <option>上海</option>  
  131.         </select>  
  132.         <input id="btnGetSelect" type="button" value="获取选中项值" οnclick="GetSelect()" />  
  133.         <input id="btnSetSelect" type="button" value="设置选中项值" οnclick="SetSelect()" />  
  134.         <input id="Button1" type="button" value="添加Option" οnclick="AddSelectOptions()" />  
  135.         <input id="Button2" type="button" value="移除选中项" οnclick="RemoveSelectOptions()" />  
  136.         <input id="Button3" type="button" value="清空下拉框" οnclick="ClearSelectOptions()" />  
  137.     </div>  
  138.           
  139.           
  140.          
  141.       
  142.     <div>  
  143.           
  144.         <input id="text1" name="text1" type="text" value="文本框" />  
  145.         <input id="btnGetText" type="button" value="获取Text值" οnclick="GetText()" />  
  146.         <input id="btnSetText" type="button" value="设置Text值" οnclick="SetText()" />  
  147.     </div>  
  148.     </form>  
  149. </body>  
  150. </html> 
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值