【发布时间】: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