我用一些html:table,複選框填充元素。 我在我的複選框中有一個onclick事件,但該頁面似乎沒有在聽點擊事件。我懷疑這是因爲它是在javascript checkall()函數被放入DOM之後加載的。jquery bind vs live
這是我有:
<select name="showcuslist" id="showcuslist">
<option value="0">select a customer</option>
<option value="1">John Smith</option>
<option value="2">Jane Smith</option>
</select>
<div id="loadtables">---ajax loads stuff into here---</div>
<script language="javascript" type="text/javascript">
$(document).ready(function(){
$("#showcuslist").change(function(){
$.ajax({
data:"formId=grabcusinfo&cusid="+$(this).val(),
success:function(response){ eval(response); }
});
});
});
function checkall(){
$(".checkboxes").attr("checked",true);
}
</script>
簡單地說,當我得到這個頁面,我可以從下拉菜單中選擇一個客戶。沒什麼太複雜的。
然後,Jquery調用服務器上的一個頁面來獲取關於該客戶的一些信息。 信息實際上是一個Notes列表,所以沒有什麼大的。但我將這些註釋格式化爲一個html表格。
該html表中的第一列有複選框,對應於每個Note記錄。 我也在頂部放了一個Master複選框,所以如果我選中該複選框,所有的複選框都會被選中。
然而,html表格載入驚人的主複選框不起作用。
這裏的HTML表格予加載到
<?php
$alltables='<table>';
$alltables.='<tr>';
$alltables.='<th><input type="checkbox" id="checkall" class="checkall" onclick="checkall();" /></th>';
$alltables.='<th>Notes Title</th>';
$alltables.='</tr>';
$alltables.='<tr>';
$alltables.='<td><input type="checkbox" id="someID" class="checkall" /></td>';
$alltables.='<td>Notes Title Text...</td>';
$alltables.='</tr>';
$alltables.='</table>';
$("#loadtables").html("'.addslashes($alltables).'");
?>
的checkall();函數工作正常,如果我加載表時加載頁面,但不是當我加載一個新的數據表與新表。
我已經被告知這是live()或bind()幫助出來的地方,但我真的不理解jquery文檔。
見一些通用的基準 [http://stackoverflow.com/questions/2690370/live-vs-bind/7692651#7692651][1] [1]:HTTP:/ /stackoverflow.com/questions/2690370/live-vs-bind/7692651#7692651 –