简体   繁体   中英

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

Struggling to wire up jquery in a new VS 2010 asp.net project.

Created a new Web App Project (with master page & other defaults) Drug jquery from the Scripts folder into Site.Master file and got:

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

(between and ) Added a textbox and this button to Default.aspx:

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

Hit F5, which launched the app and opened the page in firefox Enabled firebug. On the script tab it lists jquery-1.4.1.min and shows it's contents. Hit the button, which opens the debugger paused on the 'debugger;' statement Clicked to step $get() and got the message: ReferenceError $get is not defined.

Any help greatly appreciated. Everything looking right until the last click.

I think what you need to do is something like:

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

I am assuming your text box field has a name TextBox1 but you could just replace name with whatever attribute you have set to TextBox1 .

You need to read the jQuery documentation. $.get is used to perform an HTTP GET request from the server.If you need to set a value of TextBox on button click you should do something like this:

<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>

First of all jQuery don't have any $get function.

If I'm right you want to get the some textbox with ID that equals TextBox1 and update its value. So if you use server-side <asp:TextBox ID="TextBox1" runat="server"/> I think you have two possible solutions:

1.Save TextBox1 's ClientID into JavaScript variable and than use it:

<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.Change your server-side control <asp:TextBox .../> to client-side control <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()" />

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM