2009-10-06 93 views
0

我有我的JQuery返回null問題。

這裏是我的JQuery(其中包含在.js)....

$(document).ready(function() {   
var chkBox = $("#gvEntryPoints input[id$='cbxIncludeAll']");   
chkBox.click(function() {     
    $("#gvEntryPoints input[type='checkbox']").attr('checked', chkBox.is(':checked')); 
});   

// To deselect CheckAll when a GridView CheckBox is unchecked   
$("#gvEntryPoints INPUT[type='checkbox']").click(function(e) {    
    if (!$(this)[0].checked) {     
     chkBox.attr("checked", false);    
    }   
});  

}

看樣子chkBox從未被分配,因此從來就沒有被分配一個click事件。

這是我的HTML ...

<asp:GridView CssClass="GridView" ID="gvEntryPoints" runat="server" AutoGenerateColumns="false"> 
      <Columns> 
       <asp:TemplateField Visible="false"> 
        <ItemStyle CssClass="GridView_Item" /> 
        <ItemTemplate> 
         <asp:Label runat="server" ID="lblEntryPointListItemId" Text='<%# Eval("EntryPointListItemId") %>'/> 
        </ItemTemplate> 
       </asp:TemplateField> 
       <asp:TemplateField Visible="false"> 
        <ItemStyle CssClass="GridView_Item" /> 
        <ItemTemplate> 
         <asp:Label runat="server" ID="lblEntryPointId" Text='<%# Eval("EntryPointId") %>'/> 
        </ItemTemplate> 
       </asp:TemplateField> 
       <asp:TemplateField HeaderText="Include"> 
        <ItemStyle CssClass="GridView_Item" /> 
        <HeaderTemplate> 
         <asp:CheckBox runat="server" ID="cbxIncludeAll" CssClass="label" Checked="true" Text="Include<br/>All" TextAlign="Left" /> 
        </HeaderTemplate> 
        <ItemTemplate> 
         <asp:CheckBox runat="server" ID="cbxEPInclude" name="EPInclude" CssClass="EPCheckBox" Checked="true" /> 
        </ItemTemplate> 
       </asp:TemplateField> 
      </Columns> 
     </asp:GridView> 

我的HTML位於.ascx中。 .ascx包含在.aspx內容頁面上。我擁有包含在Master頁面上的JQuery庫(我已經在頭部,身體頂部和身體底部嘗試過)。

+0

這是我見過的最奇怪的HTML *。 – nickf 2009-10-06 14:24:14

+0

...也就是說,你可以發佈一些*真正的* HTML?喜歡,頁面的來源? – nickf 2009-10-06 14:25:45

回答

3

問題在於複選框將不會在客戶端使用cbxIncludeAll標識進行呈現。它將有一個由asp.net生成的ID。像crtl_Gridview1_001_cbxIncludeAll或類似的東西。看看客戶端來源,看看名字是什麼。

還有一個clientID屬性關閉您可以用來獲取客戶端ID的每個控件。

+0

嗯......我已經看到了太多的壞帖子,至於使用ID這樣的。 當我使用$(「input [type ='checkbox']」)進行搜索時,我也返回null,這個語句有什麼問題。 – Adam 2009-10-06 15:56:51

+0

我有一個GridView與一個CheckboxAll在標題中,然後在每個模板中我有一個複選框。我如何編寫JQuery以便CheckboxAll將檢查\取消選中Gridview中的所有複選框? – Adam 2009-10-06 16:06:57