문제

I have a table that has its rows generated by variables in my software and I want the rows to highlight when they are clicked. Here is my code:

$("#varTable > tbody:last").append("<tr style='border: 1px dotted black;'  onclick=\"rowClick('" + newVariable.Name + "','" + this + "')\">><td><img id='Img12' src='images/Delete.png' width='35' height='35' style='margin-left:0px; margin-top: 10px;'></td><td>New Variable</td><td>Global</td><td></td><td><center>0</center></td><td><form action=''>" +
    "<center><input type='checkbox' name='RetainValue' value='Retain'></center></form></td><td width='100px'><button type='button' onclick='editVariable()'>Edit</button></td></tr>"); 

}

function rowClick(name, row){
console.log(row);
selectVariable = name; 
$("#selName").html("Selected Variable: <font color='red'>" + selectVariable + "</font> Filter By: <button type='button' onclick='addVariable()'>Add Variable</button>");
$(row).css("border", "1px solid black");

}

My issue is when I pass "this" into the function rowClick it comes up as [window object] and not the tr that the onclick is located in. I also tried using this.element but that said it was undefined. Thanks for any help I can get.

도움이 되었습니까?

해결책

You need to pass this as part of the string

$("#varTable > tbody:last").append("<tr style='border: 1px dotted black;'  onclick=\"rowClick('" + newVariable.Name + "',this)\">><td><img id='Img12' src='images/Delete.png' width='35' height='35' style='margin-left:0px; margin-top: 10px;'></td><td>New Variable</td><td>Global</td><td></td><td><center>0</center></td><td><form action=''>" +
    "<center><input type='checkbox' name='RetainValue' value='Retain'></center></form></td><td width='100px'><button type='button' onclick='editVariable()'>Edit</button></td></tr>"); 

You need to evaluate this at the time of the event. Currently, you're evaluating it at the time of the append.

다른 팁

Change:

 rowClick('" + newVariable.Name + "','" + this + "')

to this:

 rowClick('" + newVariable.Name + "',this)
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top