【问题标题】:Get ID from Element/Tag从元素/标签获取 ID
【发布时间】:2019-07-19 20:28:46
【问题描述】:

这是一个给定的例子:

<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: [object 对象]

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

ratingTdText html(): textarea style="width:300px" id="xxx_001 name="xxx_001">

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

ratingTdText only id attr(): undefined

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

ratingTdText only id prop():

【问题讨论】:

  • 添加整个代码以获得更好的清晰度
  • 您好,我尝试了这一功能。希望这会澄清!
  • 试试这个内部函数从文本区域获取 id console.log(ratingTdText.find('textarea').attr('id')); // 如果我是正确的,基本上你把数据放到文本区域而不是选择 td
  • 你好,Harsha,你是对的。 textarea 在标签 td 中。这是正确的答案。非常感谢!

标签: html html-table datatable prop


【解决方案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 尝试使用它可能会起作用

【讨论】:

  • 感谢回复。试过这个:console.log("ratingTdText only id attr():"+ratingTdText.attr(this));输出:非法调用
  • 试过这个:console.log("ratingTdText only id attr(): "+ratingTdText.attr($(this)) );输出:未捕获的 DOMException:无法在“元素”上执行“setAttribute”:“0”不是有效的属性名称。
  • $('textarea').attr('id') 你可以直接用这个
  • joshi thx 回复,但如果我使用这个,选择呢?因为我只需要选择 td 来从 textarea 获取 id 属性。
【解决方案2】:

来自 Harsha 的解决方案:

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

工作得很好!非常感谢。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-11-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-23
    • 2020-02-18
    • 2012-05-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多