Basically my Jquery isn't firing on my onclick
of the button in my ASP.NET , I am new to using Jquery so I'm not sure if my syntax is correct. I am obviously calling it from the <body>
via the onload
. My code is below so I'm just seeking help tryign to find why it won't load , so my actually question is: Why won't my Jquery function load from my button?
<head runat="server">
<title>Fire Test</title>
<link id="stylesheet" href="ControlStyle.css" rel="stylesheet" type="text/css" runat="server" />
<meta name="viewport" content="width=device-width; initial-scale = 1.0; maximum-scale=1.0; user-scalable=no" />
<script type="text/javascript" src="~/Production/jquery.js"></script>
<style type="text/css">
.style1
{
font-size: x-large;
}
</style>
<script type="text/javascript">
function init() {
$("#OkBtn").on("click", alertFire);
function alertFire() {
alert("Update Successful!");
}
}
</script>
</head>
<body background="../icons/Building-Confidence-logo.jpg" onload="init();">
try
$("#<%=OkBtn.ClientID%>").on("click", alertFire);
Also you do not need to create the function separately
you can go:
$("#<%=OkBtn.ClientID%>").on("click", function(){
alert("Update Successful!");
});
update
Instead of using init on the body load why not use jquery load event
<script>
$(function(){
$("#<%=OkBtn.ClientID%>").on("click", function(){
alert("Update Successful!");
});
});
</script>
EDIT
One thing that it might be is if jQuery is not loading properly. Are you getting javascript errors saying that $ is undefined
.
Try getting rid of all script blocks an put this in
<script>
$(function(){
alert("Hello");
});
</script>
if this does not pop up "Hello" on each page refresh then the issue is with this line and the loading of jquery
<script type="text/javascript" src="~/Production/jquery.js"></script>
You need to bind your onclick in the $(function() { });
So:
$(function(){
$("#<%= OkBtn.ClientID %>").on("click", alertFire);
});
Edit:
In line with your edit, remove the Init from your body
load.
Try this:
$(document).ready(function(){
$("#OkBtn").click(function() {
alert("Update Successful!");
});
});
Do not put it inside your init
function
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.