获取单击的特定按钮的ID。

时间:2017-11-02 05:25:05

标签: javascript jquery angular typescript

我有一个表,在表格中,其中一列包含一个按钮,每一行按钮ID都不同。 id使用angular {{}}进行数据绑定。在按钮元素上,我有一个名为MarkasRead()的函数,将在单击时调用。当我尝试为它检索id时,它显示未定义,我真的需要在函数调用中的id做更多的工作。

列出的是表格代码和函数调用。

 chmod(UPLOAD_DIR . $name, 0644);

6 个答案:

答案 0 :(得分:1)

您可以这样做。从按钮事件处理程序

中传递$ event对象
<button id="{{message.$key}}" *ngIf="message.readStatus == '0'" type="submit" class="btn btn-success" (click)="MarkasRead($event)">Mark as Read</button>

在组件

MarkasRead(event) {
    var target = event.target || event.srcElement || event.currentTarget;
    var elemId = target.attributes.id;
  }

答案 1 :(得分:1)

public async Task<bool> MyBackgroundTask(){
    IsLoading=true
    //your background task
    if(successful){
        IsLoading = false
    }
}

答案 2 :(得分:1)

要执行此操作,您需要将id作为参数传递给MarkasRead函数,如&#34; MarkasRead(message。$ key)&#34; ,然后将下面的函数定义为:

function MarkasRead(value)
{
  alert(value);
}

答案 3 :(得分:0)

您可以在MarkasRead函数定义参数并传递this

function MarkasRead(el) {
  alert(el.id)
}

(click)="MarkasRead(this)"

答案 4 :(得分:0)

尝试更改

getCurrentThreadId

(click)="MarkasRead()" 

使用功能

(click)="MarkasRead(event)"

答案 5 :(得分:0)

<table *ngIf="receivedMessages?.length > 0;else noReceivedMessages" class="table table-responsive table-bordered animated bounceIn" style="table-layout: fixed;width:100%; word-wrap:break-word;">
              <thead>
                <tr>
                  <th></th>
                  <th>From</th>
                  <th>Date</th>
                  <th>Subject</th>
                  <th></th>
                  <th></th>


                </tr>
              </thead>
              <tbody *ngFor="let message of receivedMessages">
                <tr *ngIf="message.mDeleted == '0' ">
                  <td><i style="color:red" class="fa fa-exclamation" aria-hidden="true"></i></td>
                  <td> {{message.messageFrom}}</td>
                  <td> {{message.createdDate}}</td>
                  <td> {{message.subject}}</td>
                  <td><a style="width:100%;background-color:tomato;color:white" [routerLink]="['/message/'+message.$key]" href="" class="btn">
                    <i class="fa fa-arrow-circle-o-right"></i> Details</a></td>
                  <td> <button id="{{message.$key}}" *ngIf="message.readStatus == '0'" type="submit" class="btn btn-success" (click)="MarkasRead({message.$key)">Mark as Read</button>
                  </td>
                </tr>

              </tbody>
            </table>

MarkasRead(id){   警报(ID) }

相关问题