[英]Listener isn't working on my ASP.NET button from my Jquery
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. 基本上,我的Jquery不会触发我在ASP.NET中的按钮的
onclick
,我对使用Jquery是陌生的,因此不确定我的语法是否正确。 I am obviously calling it from the <body>
via the onload
. 我显然是通过
onload
从<body>
调用它的。 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? 我的代码在下面,所以我只是在寻求tryign来查找为什么不加载它,所以我的实际问题是:为什么我的Jquery函数不会从我的按钮加载?
<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 与其在主体负载上使用init,不如使用jquery load事件
<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. 这可能是一件事,如果jQuery无法正确加载。 Are you getting javascript errors saying that
$ is undefined
. 您是否收到JavaScript错误,说
$ 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 如果在每次刷新页面时都没有弹出“ Hello”,则问题在于此行和jQuery的加载
<script type="text/javascript" src="~/Production/jquery.js"></script>
You need to bind your onclick in the $(function() { });
您需要将onclick绑定到
$(function() { });
So: 所以:
$(function(){
$("#<%= OkBtn.ClientID %>").on("click", alertFire);
});
Edit: 编辑:
In line with your edit, remove the Init from your body
load. 根据您的编辑,从
body
负荷中移除Init。
Try this: 尝试这个:
$(document).ready(function(){
$("#OkBtn").click(function() {
alert("Update Successful!");
});
});
Do not put it inside your init
function 不要将其放在您的
init
函数中
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.