如何从ASP按钮调用JQuery的? [英] How to call JQuery from ASP Button?
问题描述
我一直在试图调用从 Button1的
JQuery的code,但每当我按一下按钮没有任何反应。我怀疑有一些东西在这里失踪。
<%@页面语言=C#AutoEventWireup =真codeFILE =Default.aspx.cs继承=_默认%GT;!< DOCTYPE HTML PUBLIC - // W3C // DTD XHTML 1.0过渡// ENhttp://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">< HTML的xmlns =http://www.w3.org/1999/xhtml>
<头=服务器>
<标题>< /标题>
<脚本类型=文/ JavaScript的SRC =脚本/ jQuery的-1.4.1.js/>
<脚本类型=文/ JavaScript的SRC =脚本/ jQuery的-1.4.1.min.js/>
<脚本类型=文/ JavaScript的>
$(文件)。就绪(函数(){
$(#Button1的)。点击(函数(){
警报(你好);
});
});
< / SCRIPT>
< /头>
<身体GT;
<表ID =form1的=服务器>
< DIV =服务器>
< ASP:按钮的ID =Button1的=服务器文本=按钮/>
< / DIV>
< /表及GT;
< /身体GT;
< / HTML>
请帮我,因为我是新来的Jquery与ASP
更新!
呈现的HTML
<!DOCTYPE HTML PUBLIC - // W3C // DTD XHTML 1.0过渡// ENhttp://www.w3.org/TR/xhtml1/DTD/ XHTML1-transitional.dtd>< HTML的xmlns =http://www.w3.org/1999/xhtml>
< HEAD><标题>< /标题>
<脚本类型=文/ JavaScript的SRC =脚本/ jQuery的-1.4.1.min.js/>
<脚本类型=文/ JavaScript的>
$(文件)。就绪(函数(){
$(#B1)。点击(函数(){
警报(你好);
});
});
< / SCRIPT>
< /头>
<身体GT; <表格名称=Form1的方法=邮报行动=Default.aspx的ID =form1的ENCTYPE =的multipart / form-data的>
< DIV>
<输入类型=隐藏的名字=__ VIEWSTATEID =__ VIEWSTATE value=\"/wEPDwULLTExNzQxODc4NzgPZBYCAgMPFgIeB2VuY3R5cGUFE211bHRpcGFydC9mb3JtLWRhdGEWAgITDzwrAA0AZBgBBQlHcmlkVmlldzEPZ2S859s2HXOd3dMN1a3AFBBA24YTWA==\" />
< / DIV>< DIV> <输入类型=隐藏的名字=__ EVENTVALIDATIONID =__ EVENTVALIDATIONVALUE =/ wEWBwLCh8GMCAKl1bK4CQK1qbSRCwKM54rGBgK7q7GGCALWlM + bAgLe7 + btDB1tn9tYIpN00SJIvx3i3VxCTOP7/>
< / DIV>
<输入类型=提交名称=B1值=按钮ID =B1/> < /表及GT;< /身体GT;
< / HTML>
您可以尝试添加类按钮元素,并找到这方面的div:
的Javascript:
$(文件)。就绪(函数(){ 。变量$背景= $('#containerForm')找到('形式');
$ context.find('myButton的')。绑定(点击,函数(事件){
警报(你好);
});
});
HTML:
<身体GT;
< DIV ID =containerForm>
<表ID =form1的=服务器>
< DIV =服务器>
< ASP:按钮的ID =Button1的=服务器文本=按钮类=myButton的/>
< / DIV>
< /表及GT;
< / DIV>
< /身体GT;
编辑:
,或者找到这个选择 $ context.find('输入[姓名* =Button1的]')
,使用环境是有更好的表现按钮元素。
修改二:
您code工作做好(看到这个)。您应该验证文件脚本/ jQuery的-1.4.1.min.js
加载罚款,则可能被定位在不是同一级别的路径文件夹的脚本
,根据不同的级别,或由于本设置项目,你可以把脚本/ jQuery的,1.4.1.min .js文件
,但你必须要小心,如果你有 /MyProyect/Scripts/jquery-1.4.1.min.js
。
要验证这一点,您可以使用提琴手,并找到http状态$ C $使用C 400。
I have been trying to call JQuery code from Button1
but whenever I click the button nothing happens. I suspect there is something is missing here.
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!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 type="text/javascript" src="Scripts/jquery-1.4.1.js"/>
<script type="text/javascript" src="Scripts/jquery-1.4.1.min.js"/>
<script type="text/javascript">
$(document).ready(function(){
$("#Button1").click(function () {
alert("Hello");
});
});
</script>
</head>
<body>
<form id="form1" runat="server">
<div runat="server">
<asp:Button ID="Button1" runat="server" Text="Button" />
</div>
</form>
</body>
</html>
Please Help me as I am new to Jquery with ASP
Update! Rendered HTML
<!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><title>
</title>
<script type="text/javascript" src="Scripts/jquery-1.4.1.min.js"/>
<script type="text/javascript">
$(document).ready(function () {
$("#B1").click(function () {
alert("Hello");
});
});
</script>
</head>
<body>
<form name="form1" method="post" action="Default.aspx" id="form1" enctype="multipart/form-data">
<div>
<input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="/wEPDwULLTExNzQxODc4NzgPZBYCAgMPFgIeB2VuY3R5cGUFE211bHRpcGFydC9mb3JtLWRhdGEWAgITDzwrAA0AZBgBBQlHcmlkVmlldzEPZ2S859s2HXOd3dMN1a3AFBBA24YTWA==" />
</div>
<div>
<input type="hidden" name="__EVENTVALIDATION" id="__EVENTVALIDATION" value="/wEWBwLCh8GMCAKl1bK4CQK1qbSRCwKM54rGBgK7q7GGCALWlM+bAgLe7+btDB1tn9tYIpN00SJIvx3i3VxCTOP7" />
</div>
<input type="submit" name="B1" value="Button" id="B1" />
</form>
</body>
</html>
you can try add class to button element, and find this in div context:
Javascript:
$(document).ready(function(){
var $context = $('#containerForm').find('form');
$context.find('.mybutton').bind('click', function (event) {
alert("Hello");
});
});
Html:
<body>
<div id="containerForm">
<form id="form1" runat="server">
<div runat="server">
<asp:Button ID="Button1" runat="server" Text="Button" class="mybutton" />
</div>
</form>
</div>
</body>
Edit:
or, find button element with this selector $context.find('input[name*="Button1"]')
, using contexts is to have better performance.
Edit II:
Your code work well (see this). You should verify if the file "Scripts/jquery-1.4.1.min.js
" is loading fine, you might be positioned in a path that is not on the same level of folder "Scripts
", depending on the level or as this set up the project, could you put "Scripts/jquery-1.4.1.min.js
", but you have to be careful if you have "/MyProyect/Scripts/jquery-1.4.1.min.js
".
To verify this, you can use the "fiddler", and find http status code 400.
这篇关于如何从ASP按钮调用JQuery的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!