簡體   English   中英

ReferenceError:在asp.net vs 2010中使用jquery未定義$ get

[英]ReferenceError: $get is not defined using jquery in asp.net vs 2010

努力在新的VS 2010 asp.net項目中連接jquery。

創建一個新的Web App項目(帶有母版頁和其他默認值)將Scripts文件夾中的葯物jquery導入Site.Master文件並得到:

<script src="Scripts/jquery-1.4.1.min.js" type="text/javascript"></script>

(在和之間)在Default.aspx中添加了一個文本框和這個按鈕:

<asp:Button ID="Button1" runat="server" Text="Button" OnClientClick="debugger; $get('TextBox1').value = 'hello';" />

點擊F5,啟動應用程序並在firefox啟用的firebug中打開頁面。 在腳本選項卡上,它列出了jquery-1.4.1.min並顯示了它的內容。 點擊按鈕,打開“調試器”上暫停的調試器; 聲明單擊以執行$ get()並獲得消息:未定義ReferenceError $ get。

任何幫助非常感謝。 一切正常,直到最后一次點擊。

我想你需要做的是:

$("input[name='TextBox1']").val("hello")

我假設您的文本框字段具有名稱TextBox1但您可以使用您設置為TextBox1任何屬性替換name

您需要閱讀jQuery文檔。 $ .get用於從服務器執行HTTP GET請求。如果需要在按鈕單擊上設置TextBox的值,則應該執行以下操作:

<head runat="server">
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.3/jquery.min.js"></script>
    <script type="text/javascript">
        function setValue() {
            $("#" + "<%: TextBox1.ClientID %>").val("Hello");
            return false;
        }
    </script>
</head>
<body>
    <form id="form1" runat="server">
        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
        <asp:Button ID="Button1" runat="server" Text="Button" OnClientClick="return setValue()" />
    </form>
</body>

首先,jQuery沒有任何$get函數。

如果我是對的,你想得到一個ID等於TextBox1文本框並更新它的值。 因此,如果您使用服務器端<asp:TextBox ID="TextBox1" runat="server"/>我認為您有兩種可能的解決方案:

1.將TextBox1ClientID到JavaScript變量中,然后使用它:

<script>
    var id = "<%=TextBox1.ClientID%>";
    function onClick() { 
        debugger; 
        $('#' + id).val('hello'); 
        return false;
    };
</script>
<asp:TextBox runat="server" ID="TextBox1" />
<asp:Button ID="Button1" runat="server" OnClientClick="return onClick()" />

2.將服務器端控件<asp:TextBox .../>更改為客戶端控件<input id="TextBox1" >

<script>
    function onClick() {
        debugger; 
        $('#TextBox1').val('hello'); 
        return false;
    }
</script>
<input id="TextBox1"/>
<asp:Button ID="Button1" runat="server" Text="Button" onClientClick="return onClick()" />

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM