[英]Cannot access object properties with dot notation JS
我正在嘗試訪問在單擊按鈕后調用的 JS function 中的對象屬性,但我以許多暫定的方式收到“未定義”。
有我的 HTML:
<table id="mytable" class="mytable">
<tr>
<th>Candidate Name</th>
<th>Candidate Surname</th>
<th class="remove">Interview Type</th>
<th>Scheduled date</th>
<th class="remove">Feedback</th>
<th>Detail</th>
</tr>
<tr th:each="interview: ${interviews}">
<td th:text="${interview.candidateName}" />
<td th:text="${interview.candidateSurname}" />
<td class="remove"> <span th:if="${interview.interviewType == 1}">MOTIVAZIONALE</span>
<span th:unless="${interview.interviewType == 1}">TECNICO</span>
</td>
<td th:text="${#dates.format(interview.scheduledDate, 'dd/MM/yyyy')}"/>
<td class="remove" th:text="${interview.finalFeedback}"/>
<!-- <td><button id="detail" type="submit" th:value="${interview.interviewType}" class="cd-popup-trigger" >+</button></td> -->
<!-- th:data-parameter1="${interview.id}" onclick="GotoMotivationDetail(this.getAttribute('data-parameter1'));" -->
<td> <span th:if="${interview.interviewType == 1}"><button th:data-parameter1="${interview.motivationalFeedback}" onclick="myFunction(this.getAttribute('data-parameter1'))" type="submit" class="cd-popup-trigger">?</button></span>
<span th:unless="${interview.interviewType == 1}"><button type="submit" class="cd-popup-trigger2" >?</button></span>
</td>
</tr>
</table>
JS:
function myFunction(interview){
var standing = interview.standing;
}
任何人都有訪問“采訪”object 屬性的解決方案嗎?
如果要將整個 object 用作 arguments 到 function,則不能使用數據屬性(因為它們只是字符串)。 但是,您可以使用 javascript 內聯來提取整個 object,然后像這樣對其進行索引,例如:
<script th:inline="javascript">
var interviews = /*[[${interviews}]]*/ [];
function myFunction(idx){
var interview = interviews[idx];
var standing = interview.standing;
}
</script>
<table id="mytable" class="mytable">
<tr th:each="interview, status: ${interviews}">
<td>
<button th:data-index="${status.index}"
onclick="myFunction(parseInt(this.getAttribute('data-index')))"
type="submit"
class="cd-popup-trigger">?</button>
</td>
</tr>
</tr>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.