0
Jquery .click事件只觸發第一個按鈕!我通過數據循環並在我的html頁面上添加按鈕,並且希望每個按鈕在點擊時觸發一個對話框。但是隻有第一個按鈕可以工作!其餘的似乎沒有點擊事件。Jquery .Click事件只觸發循環中的第一個按鈕
$(document).ready(function() {
$("#btn_comment").click(function() {
$("#createComment").dialog(
{
modal: true,
height: 300,
width: 500,
buttons: {
"Create a Comment": function() {
var post_id = $(this).parent().attr("id");
var desc_to_create = $("#txtComment").val();
$.post("CreateComment", { "id": "", "username": "x", "post_id": post_id, "description": desc_to_create, "created": "" }, function (t) {
alert("Thank you! Your comment has been updated!!");
location.reload();
})
},
"Cancel": function() {
$(this).dialog("close");
}
}
}
);
})
})
<tr id='<%= Html.Encode(item.id) %>'>
<td>
<%: Html.ActionLink("Details", "Details", New With {.id = item.id})%> |
<a href="javascript://" class="delete_btn">Delete</a>
</td>
<%-- <td>
<%: item.id %>
</td>
<td>
<%: item.username %>
</td>
<td>
<%: item.title %>
</td>--%>
<td>
<%: item.description %>
</td>
<td>
<input id="btn_comment" type="button" value="Add a Comment" />
</td>
<td>
<div id="new_comment"></div></td>
</tr>
<% Next%>
你有你的頁面上有一個按鈕,btn_comment的ID? ID必須是唯一的。 – j08691
@ j08691你是對的。如果有不止一個,他應該使用一個班級。 – Norse