爲什麼只觸發第一個按鈕onclick?我2人不會觸發任何事件,當我在控制檯中點擊它們,也沒有錯誤 我有:jquery按鈕單擊觸發器只能用第一個按鈕工作
<script>
$(document).ready(function(){
var name_of_sender ="";
var text_of_sender ="";
var $dialog = $('<div></div>')
.html('This dialog will show every time!')
.dialog({
autoOpen: false,
title: 'Basic Dialog'
});
$("#edit_text").click(function() {
var butName = $(this).attr("name");
var user_id = butName.split("_")[1];
alert(user_id);
name_of_sender = $('#username_'+user_id).html();
text_of_sender = $('#text_'+user_id).html();
alert(name_of_sender);
alert(text_of_sender);
$dialog.dialog('open');
// prevent the default action, e.g., following a link
return false;
});
});
</script>
<table>
<tbody>
<tr>
<td id="username_33">test</td>
<td id="text_33">asdasdasd</td>
<td>2012-09-27</td>
<td>217.xx.xx.6</td>
<td>
<button id="edit_text" name="id_33">Edit</button>
</td>
</tr>
<tr>
<td id="username_34">test</td>
<td id="text_34">asdasdasd</td>
<td>2012-09-27</td>
<td>217.xx.xx.6</td>
<td>
<button id="edit_text" name="id_34">Edit</button>
</td>
</tr>
<tr>
<td id="username_35">test</td>
<td id="text_34">asdasdasd</td>
<td>2012-09-27</td>
<td>217.xx.xx.6</td>
<td>
<button id="edit_text" name="id_35">Edit</button>
</td>
</tr>
</tbody>
</table>
你的HTML是無效的,因爲你用同樣的'id'不止一次。這也是爲什麼jQuery不能選擇比第一個實例更多的原因。 – Sparky