jquery 查找

来源:互联网 发布:网络经营项目 编辑:程序博客网 时间:2024/06/04 17:56

jquery 查找

  $(document).ready(function() {      $("#div > select").each(function(){        $(this).bind("change",function(){          $(this).nextAll("select").each(function(){              $(this).val(0);//select option:selected          });        });      });    });



 

jquery查找radio选中项

Html 代码

 <input type="radio" name="sex" value="1" />男

 <input type="radio" name="sex" value="2" />女

 $("input[type=radio]:checked").val() 


 


 

 jquery查找select选中项

Html 代码

  <select name="bank" id="bank">
     <option value="icbc">工商银行</option>
    <option value="ebank">光大银行</option>
     <option value="bc">中国银行</option>
   </select>

 $("select option:selected").val() 如果有多个列表框 $("select[name=bank] option:selected").val() 


 


 

 jquery查找checken选中项
  

跟上边类似 由于checkbox为数组,获取时只能获取 同名字的第一个值   

Html 代码

<input type="checkbox" value="1" name="city"/> 北京

<input type="checkbox" value="2" name="city"/> 吉林

 

$("input[type=checkbox][name=city]:checked").val()//只能获取第一个所以要去循环$("input[type=checkbox][name=city]:checked").each(function(){       alert($(this).val());})

 


 

 jquery查找table中元素

Html 代码

<table id="table1">
  <tr id="tr1">
    <td>a</td>
    <td>b</td>
    <td>c</td>
  </tr>
  <tr id="tr2">
    <td>1</td>
    <td>2</td>
    <td>3</td>
  </tr>
</table>

$("#table1 tr[id=tr1] td:nth-child(3)").text()或者$("#table1 tr").each(function(){ alert($(this).children("td:eq(1)").text()); }); 





jquery的配置项说明

Html

$.ajax(function(){    url:"/reponse.jsp",// 请求连接 date:"age=m&id=001",//参数也可写到url后边    type:"post",//get 或 post    dataType:"json",//返回 JSON 数据格式 xml 返回 XML 文档; text 返回纯文本字符串; html 返回纯文本 HTML 信息;包含 script 元素; script 返回纯文本 JavaScript 代码    success:function(returnValue){ alert(returnValue);}, //返回的数据 dataType用json时注意返回的格式正确,否则会执行faile方法    faile:function(){ //失败时的操作 },    async : true,//(默认: true) 默认设置下,所有请求均为异步请求。如果需要发送同步请求,请将此选项设置为 false。注意,同步请求将锁住浏览器,用户其它操作必须等待请求完成才可以执行。    timeout: 10000,//设置请求超时时间(毫秒)    cache :true //设置为 false 将不会从浏览器缓存中加载请求信息 (默认: true)}) 


 

 


 

 jquery控制父窗口的元素

HTML代码

当用window.open()打开一个子页面时,在子页面获取父页面的值或改变值时:

父窗口:<input type="text" value="2010-1-1" id="date"/>

$("#date",window.opener.document).val() 

输出结果为: 2010-1-1

 


 

jquery建立数组

HTML代码

<div>1</div><div>second</div><div>3</div><div>fourth</div><div>5</div><div>sixth</div>

var arr=$.makeArray($("div"));alert($(v[1]).text());

输出结果为: second

数组翻转 HTML代码

var arr=$.makeArray($("div"));var turnArr=arr.reverse();alert($(turnArr[1]).text());

输出结果为: 5


 

 jquery循环

HTML代码

<div>1</div><div>second</div><div>3</div><div>fourth</div><div>5</div><div>sixth</div>

$("div").each(function(i){//一个参数    alert("第"+i+"个"+"内容是"+$(this).text());}) 

输出结果为: 第1个内容是1
             第2个内容是second
             第3个内容是3
             ...

对json格式输出 也可对数组迭代输出i为坐标号j为对应的值

 

$.each({name:"bingo",age:"25",sex:"youngMan"},function(i,j){    alert("key:"+i+",value"+j);}) 

输出结果为: name:name,value:bingo
             name:age,value:25
             name:sex,value:youngMan



原创粉丝点击