如果根据列名 id 选中复选框,则查找 id 列的值
Find the value of id column if checkbox is checked on the basis of column name id
我必须根据列名在同一行中找到其复选框被选中的 id 列的值,因为 id 列位置不固定。它可以是表中的第一个,最后一个或中间。我应用了以下代码:
<table id="rowclick2" border="1">
<tr>
<th>Select</th>
<th>Des</th>
<th>ID</th>
</tr>
<tr>
<td>
<input type="checkbox" name="vehicle" value="Bike">
</td>
<td>col 2, row 1</td>
<td>col 3, row 1</td>
</tr>
<tr>
<td>
<input type="checkbox" name="vehicle" value="Car"> </td>
<td>col 2, row 2</td>
<td>col 3, row 2</td>
</tr>
</table>
我的jquery是:
$("input[name='vehicle']").click(function() {
alert("djb");
if ($(this).is(':checked')) {
alert("dj");
if ($(this).closest('table').next('tr').find('th:contains(Jan 3)') == 'true') {
alert("bhh");
} else {
alert("cbjhj");
}
}
});
请帮助查找选中复选框的 id 值。我是jquery,javascript的新手。
我建议采用以下方法:
// binding a change event-handler to <input type="checkbox" /> elements:
$('input[type=checkbox]').change(function() {
// assuming you want this to run only when the checkbox is checked:
if (this.checked) {
// find the <th> elements:
var ID = $('th').filter(function() {
// find those <th> elements whose trimmed-text is exactly 'ID':
return $.trim($(this).text()) === 'ID';
// get the cellIndex property of that element (or the first matched element):
}).prop('cellIndex'),
// find the closest <tr> element from the checked <input />:
IDVal = $(this).closest('tr')
// find the descendant <td> elements:
.find('td')
// find the <td> with the same cellIndex as the <th>
.eq(ID)
// retrieve the text:
.text();
console.log(IDVal);
}
});
$('input[type=checkbox]').change(function() {
if (this.checked) {
var ID = $('th').filter(function() {
return $.trim($(this).text()) === 'ID';
}).prop('cellIndex'),
IDVal = $(this).closest('tr').find('td').eq(ID).text();
console.log(IDVal);
}
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table id="rowclick2" border="1">
<tr>
<th>Select</th>
<th>Des</th>
<th>ID</th>
</tr>
<tr>
<td>
<input type="checkbox" name="vehicle" value="Bike">
</td>
<td>col 2, row 1</td>
<td>col 3, row 1</td>
</tr>
<tr>
<td>
<input type="checkbox" name="vehicle" value="Car">
</td>
<td>col 2, row 2</td>
<td>col 3, row 2</td>
</tr>
</table>
但是,如果您使用类名来标识哪些单元格包含"id"关联值,例如<td class="id"></td>
,这一切都会变得简单得多:
$('input[type=checkbox]').on('change', function() {
if (this.checked) {
// find the closest <tr> element from the :checked <input />:
var idValue = $(this).closest('tr')
// find the element with the class of 'id':
.find('.id')
// retrieve the text of that element:
.text();
console.log(idValue);
}
});
$('input[type=checkbox]').on('change', function() {
if (this.checked) {
var idValue = $(this).closest('tr').find('.id').text();
console.log(idValue);
}
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table id="rowclick2" border="1">
<tr>
<th>Select</th>
<th>Des</th>
<th class="id">ID</th>
</tr>
<tr>
<td>
<input type="checkbox" name="vehicle" value="Bike">
</td>
<td>col 2, row 1</td>
<td class="id">col 3, row 1</td>
</tr>
<tr>
<td>
<input type="checkbox" name="vehicle" value="Car">
</td>
<td>col 2, row 2</td>
<td class="id">col 3, row 2</td>
</tr>
</table>
引用:
- JavaScript:
-
cellIndex
.
-
- j查询:
-
closest()
. -
eq()
. -
filter()
. -
find()
. -
jQuery.trim()
. -
on()
. -
prop()
.
-
我希望这有所帮助。请看演示。
根据我从中了解到的情况,请帮助查找选中复选框的 id 值。
$(document).ready(function(){
$(":checkbox").click(function(){
alert( $(this).val() );
});
});
http://jsfiddle.net/silkherb/tx2asmg6/2/
将 ID 添加到复选框id="first"
和id="second"
以获取元素:
<tr>
<td><input type="checkbox" name="vehicle" value="Bike" id="first"></td>
<td>col 2, row 1</td>
<td>col 3, row 1</td>
</tr>`enter code here`
<tr>
<td><input type="checkbox" name="vehicle" value="Car" id="second"> </td>
<td>col 2, row 2</td>
<td>col 3, row 2</td>
</tr>
之后,您可以使用简单的JavaScript。 .checked
检查复选框是否已标记。
if(document.getElementById("first").checked == true){
console.log("do something here 1");
}
if(document.getElementById("second").checked == true){
console.log("do something here 2");
}
相关文章:
- 如何按id查找两个类中任一类的子元素
- 通过id查找元素并附加链接
- 在图库中按ID查找图像
- 如何在角度指令测试中按 id 查找元素
- 在 Firebase 中通过对象的唯一 id 查找对象
- 按 ID 查找和元素,并在特定时间后重定向
- 使用Coffeescript和lodash按ID查找
- jQuery按ID查找上一个按钮(将具有不同的名称)
- 在节点.js中按类型和 ID 查找 XML 元素
- 如何在Angualar的jqLite元素对象中按id查找元素
- 在 json 树中按 id 查找节点
- 使用json中的id查找对象索引
- 谷歌地图:按Id查找多边形
- 在html代码中点击与通过id查找|如何将值从php添加到js
- 通过ID查找MongoDB文档
- jQuery不按ID查找
- 在数组中通过ID查找对象并进行修改
- 使用Javascript通过ID查找元素的最佳性能.数组或对象
- Javascript/NodeJS:通过对象数组/集合中的id查找特定对象的最佳方法
- 如何在对象数组中通过id查找对象