在函数中传递按钮值属性值,调用onclick()函数,动态创建按钮

时间:2016-08-14 19:03:20

标签: javascript jquery

我正在动态创建表行。在每一行中,我也创建了按钮。每行都有按钮。单击按钮我要传递值属性的值(value =“'+ patient .appointmentId +')函数getAppointment()。

<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 个答案:

答案 0 :(得分: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>