[英]Getting a table row cell value when a specific column is is clicked
我能夠通過表click事件檢索所有行值,並通過event.currentTarget.cells[4].innerText();
獲取其值event.currentTarget.cells[4].innerText();
。
但是,如果僅在單擊“ Username column
下的ID 21
時才單擊特定列,我想應用此功能。 它應該警告該行的所有單元格值。 然后,當我單擊其他列時,它不應發出警報。
這是我的代碼。 如果您遇到問題,請告知我。
<script type="text/javascript" language="javascript">
$(document).ready(function () {
$('#tableid').on('click', 'tr', function (event) {
alert(event.currentTarget.cells[0].innerText);
alert(event.currentTarget.cells[1].innerText);
alert(event.currentTarget.cells[2].innerText);
alert(event.currentTarget.cells[3].innerText);
alert(event.currentTarget.cells[4].innerText);
});
});
</script>
這是我的HTML http://jsfiddle.net/jE5UM/
我建議在沒有特定的HTML和其他信息的情況下:
$(document).ready(function () {
$('#tableid').on('click', 'tr', function (event) {
var cell = event.target,
values = $(cell).siblings().addBack().map(function(){
return $(this).text();
}).get();
alert(values);
});
});
JS小提琴演示 。
參考文獻:
嘗試
$(document).ready(function () {
$('#tableid').on('click', 'tr', function (event) {
$(this).children().each(function(){
alert($(this).text())
})
});
});
演示: 小提琴
如果你想要一個數組作為結果
$(document).ready(function () {
$('#tableid').on('click', 'tr', function (event) {
var texts = $(this).children().map(function(){
return $.trim($(this).html())
}).get();
});
});
演示: 小提琴
更新資料
$(document).ready(function () {
$('#tableid').on('click', 'tr td:nth-child(2)', function (event) {
var texts = $(this).closest('td').siblings().addBack().map(function(){
return $.trim($(this).html())
}).get();
alert(texts.join())
});
});
演示: 小提琴
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.