0

を使用しないインタラクティブなWebフォームで作業しているUpdatePanelので、JavaScriptを使用してほとんどの機能を実行しようとしています。この質問では、Javaスクリプトを取得してPageLoad()のドロップダウンリストに関数を追加する方法を理解しようとしています。

私は次のASPファイルを持っています:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <script src="Default.js" type="text/javascript"></script>
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.0/jquery.min.js" type="text/javascript"></script>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            Discovery Form Templates
            <asp:DropDownList ID="uiFormTemplates" runat="server" DataTextField="Subject" DataValueField="DiscoveryFormID" AppendDataBoundItems="true" OnChange="GetTemplateValue();">
                <asp:ListItem Text="--Select One--" Value=""/>
            </asp:DropDownList>
        </div>
        <div id="ChangePlate"></div>
    </form>
</body>
</html>

そして、このjavascript:

function GetTemplateValue()
{
var dropdown = document.getElementById("uiFormTemplates");
var SelectedOption = dropdown.options[dropdown.selectedIndex].value;

if (SelectedOption == null) {
    document.getElementById("ChangePlate").innerText = "There is nothing here.";
}
else {
    document.getElementById("ChangePlate").innerText = dropdown;
}
}

私は次のJavaScriptを使おうとしています:

$(document).ready(function () {
    $("#uiFormTemplates").onchange(function () { GetTemplateValue(); });
});

OnChange="GetTemplateValue()"から削除するdropdownlistと、2番目のjavascriptメソッドを使用しても何も起こりません。私は自分のコードを間違って書いたのですか、それとも私はこれに正しい角度からアプローチしていませんか?コード批評か何らかの方向性のどちらかが今すぐ役立つでしょう、私はjsnoobです。

4

1 に答える 1

2

(使用している)jQueryを含めていると仮定すると、メソッドはありませんonchange。に変更するか、メソッドon('change', ...)を使用する必要があります。changeまた、#uiFormTemplates動作しないはずです、あなたはあなたのコントロールを使用する必要がありますClientID

それで:

$(document).ready(function () {
    $("#<%= uiFormTemplates.ClientID %>").on('change', function () { GetTemplateValue(); });
});

または:

$(document).ready(function () {
    $("#<%= uiFormTemplates.ClientID %>").change(function () { GetTemplateValue(); });
});
于 2013-02-05T16:23:52.510 に答える