te')); return $arr; } /* 遍历用户所有主题 * @param $uid 用户ID * @param int $page 页数 * @param int $pagesize 每页记录条数 * @param bool $desc 排序方式 TRUE降序 FALSE升序 * @param string $key 返回的数组用那一列的值作为 key * @param array $col 查询哪些列 */ function thread_tid_find_by_uid($uid, $page = 1, $pagesize = 1000, $desc = TRUE, $key = 'tid', $col = array()) { if (empty($uid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('uid' => $uid), array('tid' => $orderby), $page, $pagesize, $key, $col); return $arr; } // 遍历栏目下tid 支持数组 $fid = array(1,2,3) function thread_tid_find_by_fid($fid, $page = 1, $pagesize = 1000, $desc = TRUE) { if (empty($fid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('fid' => $fid), array('tid' => $orderby), $page, $pagesize, 'tid', array('tid', 'verify_date')); return $arr; } function thread_tid_delete($tid) { if (empty($tid)) return FALSE; $r = thread_tid__delete(array('tid' => $tid)); return $r; } function thread_tid_count() { $n = thread_tid__count(); return $n; } // 统计用户主题数 大数量下严谨使用非主键统计 function thread_uid_count($uid) { $n = thread_tid__count(array('uid' => $uid)); return $n; } // 统计栏目主题数 大数量下严谨使用非主键统计 function thread_fid_count($fid) { $n = thread_tid__count(array('fid' => $fid)); return $n; } ?>javascript - How to get id of a element in all the rows of a table using jQuery - Stack Overflow
最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to get id of a element in all the rows of a table using jQuery - Stack Overflow

programmeradmin3浏览0评论

My table id is "termTable". I am iterating through each row of the table. The id of textbox is "label'+ i +'" since I used for loop to generate ids randomly. The values of i will be unordered maybe after adding the rows or deleting the rows. Example: IDs are label1, label2, label3. After deleting the 2nd row, I am adding another row. So now the IDs are label1,label3, label4. I want to retrieve all the ids of all the textbox elements. How can I achieve it? Please help.

for (var i = 0; i < firstTabLength; i++) {
    var row = $("#termTable tr").eq(i);
    var id = row.find('input[type="text"]').attr('id');   // I could see id = undefined             
}

This is Html. This is for adding the new rows to table. Guess you can visualize the table structure with the help of below code.

function AddCustomTerm1() {
    len = len + 1;
    var table = document.getElementById("termTable");
    var rowCount = table.rows.length;
    var row = table.insertRow(rowCount);
    row.insertCell(0).innerHTML = '<input type="text" class="form-control" id="label' + (len - 1) + '" name="label' + (len - 1) + '" value=""><input type="hidden" id="step' + (len - 1) + '" value="1"/>';
    row.insertCell(1).innerHTML = '<select class="form-control" id="dataTypes' + (len - 1) + '" name="dataTypes' + (len - 1) + '"  onchange="changeDataType(this)"></select>'
    row.insertCell(2).innerHTML = '<input type="text" class="form-control" id="ContractTerm' + (len - 1) + '" name="ContractTerm' + (len - 1) + '" value="">';

    var DD = document.getElementById("dataTypes" + (len - 1));

    for (var i = 0; i < datatypesarray.length; i++) {
        var opt = document.createElement("option");
        opt.text = datatypesarray[i];
        opt.value = datatypesarray[i];
        DD.appendChild(opt);
    }

My table id is "termTable". I am iterating through each row of the table. The id of textbox is "label'+ i +'" since I used for loop to generate ids randomly. The values of i will be unordered maybe after adding the rows or deleting the rows. Example: IDs are label1, label2, label3. After deleting the 2nd row, I am adding another row. So now the IDs are label1,label3, label4. I want to retrieve all the ids of all the textbox elements. How can I achieve it? Please help.

for (var i = 0; i < firstTabLength; i++) {
    var row = $("#termTable tr").eq(i);
    var id = row.find('input[type="text"]').attr('id');   // I could see id = undefined             
}

This is Html. This is for adding the new rows to table. Guess you can visualize the table structure with the help of below code.

function AddCustomTerm1() {
    len = len + 1;
    var table = document.getElementById("termTable");
    var rowCount = table.rows.length;
    var row = table.insertRow(rowCount);
    row.insertCell(0).innerHTML = '<input type="text" class="form-control" id="label' + (len - 1) + '" name="label' + (len - 1) + '" value=""><input type="hidden" id="step' + (len - 1) + '" value="1"/>';
    row.insertCell(1).innerHTML = '<select class="form-control" id="dataTypes' + (len - 1) + '" name="dataTypes' + (len - 1) + '"  onchange="changeDataType(this)"></select>'
    row.insertCell(2).innerHTML = '<input type="text" class="form-control" id="ContractTerm' + (len - 1) + '" name="ContractTerm' + (len - 1) + '" value="">';

    var DD = document.getElementById("dataTypes" + (len - 1));

    for (var i = 0; i < datatypesarray.length; i++) {
        var opt = document.createElement("option");
        opt.text = datatypesarray[i];
        opt.value = datatypesarray[i];
        DD.appendChild(opt);
    }
Share Improve this question edited Feb 27, 2016 at 8:09 P Sriharsha asked Feb 27, 2016 at 7:55 P SriharshaP Sriharsha 1551 gold badge4 silver badges15 bronze badges 1
  • firstTabLength is number of rows in the table – P Sriharsha Commented Feb 27, 2016 at 7:55
Add a ment  | 

5 Answers 5

Reset to default 4

without you html format cant help much.

but try this ones.you dont even need a for loop to get all the textbox and their ids from table.

   var ids=[];
    $('#termTable input[type="text"]').each(function(){
      ids.push($(this).attr('id'))
    })

if you want ids of the textboxes in first column of the table.the try this

var ids=[];
        $('#termTable tr').each(function(){
          ids.push($(this).find('td:first-child input[type="text"]').attr('id'))
        })

The best practice is to add a specific class to all of your elements that need to be iterated.

For example add the class term-table-input to your inputs:

row.insertCell(0).innerHTML = '<input type="text" class="form-control term-table-input" id="label' + (len - 1) + '" name="label' + (len - 1) + '" value=""><input type="hidden" id="step' + (len - 1) + '" value="1"/>';

And iterate them with:

var termTableInputIDs = [];

$('.term-table-input').each(function () {
    termTableInputIDs.push($(this).attr('id'));
});

Also you could read more about writing efficient css here: https://developer.mozilla/en-US/docs/Web/Guide/CSS/Writing_efficient_CSS

Since you have more than one input[type="text"] in a row, you should take the first. Also you can use each() instead of for.

$('#termTable tr').each(function() {
    var id = $(this).find('input[type="text"]:first').attr('id');
    console.log(id);
});

You find all the input elements inside the table by using $('#termTable input') and to loop through all the selected elements you use $.each API

Use Code like below.

var InputIds = [];  //array to hold your id's
$.each($('#termTabel input[type="text"]'),function(i,v){
     InputIds.push($(this).attr('id'));
});
console.log(InputIds.toString());
//this finds all inputs which id start from "label"
$('#termTable input[id^="label"]').each(function(){ console.log(this.id); });

example: https://jsbin./lisuratere/edit?html,js,output

发布评论

评论列表(0)

  1. 暂无评论