Home > Web Front-end > JS Tutorial > body text

Detailed example of jQuery getting the value of a field in the current row selected by the check box_

小云云
Release: 2017-12-29 09:15:48
Original
1872 people have browsed it

This article mainly introduces jQuery to get the value of a field in the current row selected by the check box. Friends who need it can refer to it. I hope it can help everyone.

Without further ado, I will post the code directly for you. The specific code is as follows:


 <html>
 <head>
 <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
 <title>js</title>
 </head>
<script src="js/jquery.js"></script>
<script language="javascript">
 function getTDtext() 
 {
 var a = document.getElementsByName("r");//获取所以复选框
//第一种方法:通过checked属性确定被选中的复选框的父节点,通过children[index]来取第index个子节点td的内容
/** for(var i=0;i<a.length;i++){
  if(a[i].checked){
    var tddata=a[i].parentNode.parentNode.children[1].innerHTML;
    alert(tddata);
  }
} **/
//第二种方法:通过checked属性确定被选中的复选框的父节点,遍历父节点下所以的子节点td,当td的name等于某值时,获取该td下的内容
for(var i=0;i<a.length;i++){
if(a[i].checked){
  var tddata=a[i].parentNode.parentNode;
  $(tddata).find("td").each(function(){
    if(this.getAttribute("name")=="z"){
      alert(this.innerHTML);
    }
  });
}
}
}
 </script>
 <body>
 <table id="test_table">
 <tr><td><input type="checkbox" name="r" value="1"></td><td name="z">a</td><td name="x">b</td></tr>
 <tr><td><input type="checkbox" name="r" value="2"></td><td name="z">b</td><td name="x">b</td></tr>
 <tr><td><input type="checkbox" name="r" value="3"></td><td name="z">c</td><td name="x">b</td></tr>
 <tr><td><input type="checkbox" name="r" value="4"></td><td name="z">d</td><td name="x">b</td></tr>
 <tr><td><input type="checkbox" name="r" value="5"></td><td name="z">e</td><td name="x">b</td></tr>
 <tr><td><input type="checkbox" name="r" value="6"></td><td name="z">f</td><td name="x">b</td></tr>
 </table>
 <input type="button" onclick="getTDtext()" value="button"/>
</body>
</html>
Copy after login

Related recommendations:

Get PHP’s memory usage

Detailed example of AngularJS traversal to obtain array elements

JS implementation of methods to obtain various pinyin types

The above is the detailed content of Detailed example of jQuery getting the value of a field in the current row selected by the check box_. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!