从元素/标签获取ID

时间:2019-02-26 10:13:09

标签: html html-table datatable id prop

这是一个给定的示例:

<textarea style="width:300px" id="xxx_001" name="xxx_001"></textarea>

我想获取ID的值

  

“ xxx_001”

这是我的代码:

$('.buttonclick').on('click', function() {

    var textareaval = $('#textareavalue').val();

    $("tbody").find("tr").each(function() { //get all rows in table

        var ratingTdText = $(this).find('td.selected'); // where in one column is selected

        // this is what i get textarea tag/element see above
        console.log("ratingTdText html(): "+ratingTdText.html() );  

        // i want to get only xxx_001
        console.log("ratingTdText only id: "+ratingTdText.attr('id') ); // doesn't happen
        console.log("ratingTdText only id: "+ratingTdText.prop('id') ); // doesn't happen

        var only_id = ratingTdText.attr('id');

        $("#textareaset_"+only_id).text(textareaval);
        $('#checkboxset_' + only_id).prop('checked', true);
        $('#selectoptionset_'+only_id).val('1');
    });
});

请,您有解决办法吗?

以下是一些输出:

console.log("ratingTdText: "+ratingTdText );
  

ratingTdText:[对象对象]

console.log("ratingTdText html(): "+ratingTdText.html() );  
  

ratingTdText html():textarea样式=“ width:300px” id =“ xxx_001 name =” xxx_001“>

console.log("ratingTdText only id attr(): "+ratingTdText.attr('id') );
  

ratingTdText仅id attr():未定义

        console.log("ratingTdText only id prop(): "+ratingTdText.prop('id') );
  

ratingTdText仅id prop():

2 个答案:

答案 0 :(得分:1)

$('.buttonclick').on('click', function() {

var textareaval = $('#textareavalue').val();

$("tbody").find("tr").each(function() { //get all rows in table

    var ratingTdText = $(this).find('td.selected'); // where in one column is selected

    // this is what i get textarea tag/element see above
    console.log("ratingTdText html(): "+ratingTdText.html() );  

    // i want to get only xxx_001
    console.log("ratingTdText only id: "+ratingTdText.attr(this) ); // doesn't happen
    console.log("ratingTdText only id: "+ratingTdText.prop(this) ); // doesn't happen

    var only_id = ratingTdText.attr('id');

    $("#textareaset_"+only_id).text(textareaval);
    $('#checkboxset_' + only_id).prop('checked', true);
    $('#selectoptionset_'+only_id).val('1');
});

});

代替使用id的od尝试使用它可能会起作用

答案 1 :(得分:0)

Harsha的解决方案:

console.log("ratingTdText only id attr(): "+ratingTdText.find('textarea').attr('id') );
console.log("ratingTdText only id prop(): "+ratingTdText.find('textarea').prop('id') );

做的很好!非常感谢。