2016-08-14 54 views
0

我正在動態創建表格行。在每一行中,我也在創建按鈕。表示每行都有按鈕。單擊按鈕我想通過value屬性的值(值=​​「 '+ patient.appointmentId +')在功能getAppointment()。在調用onclick()函數的函數中傳遞按鈕值屬性值,動態創建按鈕

<button type="button" 
value="'+patient.appointmentId+' "id="cancel-appointment" 
onclick = "getAppointment()" class="fa fa-remove btn-transparent"> 
</button> 

想這

<button type="button" 
value="'+patient.appointmentId+' "id="cancel-appointment" 
onclick = "getAppointment(patient.appointmentId)" class="fa fa-remove btn-transparent"> 
</button> 

回答

1

可以使用傳遞值this.value:

<button type="button" 
    value="'+patient.appointmentId+' "id="cancel-appointment" 
    onclick = "getAppointment(this.value)" class="fa fa-remove 
    btn-transparent"> 
    </button> 

如果屬性不同,可以使用this.getAttribute(「value」)。

function getAppointment(v) { 
 
    console.log('value=' + v); 
 
}
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css"> 
 
<script src="https://code.jquery.com/jquery-1.12.1.min.js"></script> 
 
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script> 
 

 
<button type="button" 
 
     value="'1' "id="cancel-appointment" 
 
     onclick = "getAppointment(this.value)" class="fa fa-remove btn-transparent">Button1 
 
</button> 
 
<button type="button" 
 
     value="'2' "id="cancel-appointment1" 
 
     onclick = 'getAppointment(this.getAttribute("value"))' class="fa fa-remove btn-transparent">Button2 
 
</button>

相關問題