Javascript 如何在JQuery中构造元素的id并对其执行操作?
我的表格行中有一个按钮,它有一个idJavascript 如何在JQuery中构造元素的id并对其执行操作?,javascript,jquery,Javascript,Jquery,我的表格行中有一个按钮,它有一个idbtn\u number\uu$I。单击按钮后,我想显示id为row_$I的表格行,该行最初是隐藏的 以下是创建两个表行的PHP代码: echo "<tr><td>"."<button id= \"btn_number_$i\" class='btn info toggler' >Info <i class='icon-arrow-down'></i></button>"."<
btn\u number\uu$I
。单击按钮后,我想显示id为row_$I
的表格行,该行最初是隐藏的
以下是创建两个表行的PHP代码:
echo "<tr><td>"."<button id= \"btn_number_$i\" class='btn info toggler' >Info <i class='icon-arrow-down'></i></button>"."</td></tr>";
echo "<tr id='row_$i' class='info_row'><td>Hello There!!</td></tr>";
感谢您的帮助。您没有使用包含您构建的id的变量
rowId
,而是使用rowId
字符串文字。您拥有的选择器将使用id=rowId
改变
到
'abcdefg'.split('').pop()
给你g
。问题不在于没有得到最后一个值,而是如何计算相关元素的id。但多亏了这是一种获取字符串最后一个元素的新方法,我以前没有新建它。所以adil$('#“另一个字符串”\u literal'))这就是阻碍我得到正确结果的原因。这里的逗号顺序是错误的$('#“另一个字符串”_literal')。字符串常量是$('#另一个字符串”_literal'),变量另一个字符串是$('#'+另一个字符串”_literal)
$(function () {
$('.info_row').hide(); // first I need to hide all the second rows.
$('.toggler').click(function () {
var currentId = $(this).attr('id');
console.log(currentId);
var lastChar = currentId.substr(currentId.length - 1); //Herein I am trying to extract the last character from btn_number_$i which is $i and then appending it with 'row_'
var rowId = 'row_' + lastChar;
console.log(rowId); // I am able to get the current value in console log.
$('#rowId').show(); // But even after all that i am not able to show the element.
});
});
$('#rowId').show();
$('#' + rowId).show();