3

私はWeb開発に不慣れなので、まだ基礎を学んでいます。

ASP .NET ページに jQuery を追加したいだけです。

ヘッダーでは、必要なものを参照しています。

<head id="Head1" runat="server">
        <link href="Content/Site.css" rel="stylesheet" type="text/css"/>
        <script type="text/javascript" src="Scripts/jquery-2.0.2.js" ></script>
        <script type="text/javascript" src="~/HeaderCheckBoxSelections.js"></script>
</head>

これは~/HeaderCheckBoxSelections.js次のようになります。

function SelectAllCheckBoxes(cbSelect) {
    $('#<%=gvShows.ClientID%>').find("input:checkbox").each(function() {
        if (this != cbSelect) {
            this.checked = cbSelect.checked;
        }
    });
}

イベントはこれによってトリガーされます。

<HeaderTemplate>
     <asp:CheckBox ID="cbSelectAll" runat="server" onclick="javascript:SelectAllCheckBoxes(this);"/>
</HeaderTemplate>

しかし、チェックボックスをクリックすると、次のように表示されます。

未処理の例外...

0x800a1391 - JavaScript runtime error: 'SelectAllCheckBoxes' is undefined.

ここで何か不足していますか?jQuery を適切に参照していませんか?

4

2 に答える 2

3

のjqueryセレクターのデータバインディング

$('#<%=grdOperation.ClientID%>')

別の JavaScript ファイルでは機能しません。正しくレンダリングするには、そのデータバインディングを aspx マークアップに含める必要があります。

次のような変数に格納することで、そのデータバインディングを aspx に移動できます。

<head id="Head1" runat="server">
    <link href="Content/Site.css" rel="stylesheet" type="text/css"/>
    <script type="text/javascript" src="Scripts/jquery-2.0.2.js" ></script>
    <script type="text/javascript" src="~/HeaderCheckBoxSelections.js"></script>
    <script type="text/javascript">
        var grdOperationId = '<%=grdOperation.ClientID%>';
    </script>
</head>

そして、それを JavaScript の変数で参照します。

または、ページに grdOperation が 1 つしかない限り、「id が終わる」セレクターを使用して js コードで選択することもできます。そのような:

function SelectAllCheckBoxes(cbSelect) {
    $('[id$=grdOperation]').find("input:checkbox").each(function() {
        if (this != cbSelect) {
            this.checked = cbSelect.checked;
        }
    });
}
于 2013-07-08T17:33:20.577 に答える