我有一个 btnSave 按钮,我想通过 jquery 单击输入按钮 btnmore 来调用此 btnSave_Click 方法,我该怎么做
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
<title>Jquery</title>
<script type="text/javascript" src="scripts/jquery-1.4.2.js"></script>
<script type="text/javascript"> jQuery(document).ready(function () {
$('#btnmore').click(function () {
$('<%=btnSave.ClientID%>').click();
alert('done');
$.ajax({type: 'POST',
contentType: 'application/json; charset=utf-8',
data: '{}',
dataType: 'json',
url: 'Default.aspx/btnSave_Click',
success: function(result){
alert(result);} });
});
});</Script>
</head>
<body>
<form id="form1" runat="server">
<asp:scriptManager id="script1"></asp:scriptManager>
<div>
<table>
<tr>
<td>
<asp:Button ID="btnSave" runat="server" Text="Save" OnClick="btnSave_Click" />
<input type="button" id="btnmore" value="More" />
</td>
</tr>
</table>
</div>
</form>
</body>
</html>
服务器端代码
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Services;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
[WebMethod]
public static string MyMethod()
{
return "abc";
}
[WebMethod]
protected void btnSave_Click(object sender, EventArgs e)
{
Response.Write("abc");
}
}
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
<title>Jquery</title>
<script type="text/javascript" src="scripts/jquery-1.4.2.js"></script>
<script type="text/javascript"> jQuery(document).ready(function () {
$('#btnmore').click(function () {
$('<%=btnSave.ClientID%>').click();
alert('done');
$.ajax({type: 'POST',
contentType: 'application/json; charset=utf-8',
data: '{}',
dataType: 'json',
url: 'Default.aspx/btnSave_Click',
success: function(result){
alert(result);} });
});
});</Script>
</head>
<body>
<form id="form1" runat="server">
<asp:scriptManager id="script1"></asp:scriptManager>
<div>
<table>
<tr>
<td>
<asp:Button ID="btnSave" runat="server" Text="Save" OnClick="btnSave_Click" />
<input type="button" id="btnmore" value="More" />
</td>
</tr>
</table>
</div>
</form>
</body>
</html>
Server side code
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Services;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
[WebMethod]
public static string MyMethod()
{
return "abc";
}
[WebMethod]
protected void btnSave_Click(object sender, EventArgs e)
{
Response.Write("abc");
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
为什么不直接使用:
请注意,您将需要获取 btnSave 的实际名称,因为它可能位于命名容器中。像这样的东西应该得到正确的 id:
Why not just use:
Note that you will need to get the actual name of btnSave, because it will probably be in a naming container. Something like this should get the correct id:
这可以作为替代解决方案。在同一个按钮上您可以调用 2 个点击函数;一个客户端和另一个服务器端。
buttonClientValue()
函数应返回 true 或 false。如果返回true,则仅执行btnSave_Click
,否则不执行。This can be an alternate solution. On the same button you can call 2 click functions; one client side and another server side.
buttonClientValue()
function should return true or false. If returns true than onlybtnSave_Click
will be executed, otherwise not.这就是我喜欢做的事情。由于您无法真正控制 ASP.NET 分配的 ID,因此您需要在与要单击的按钮相同的页面中添加一些 javascript 代码。您可以将该代码包装在一个具有已知名称的函数中,如果需要,您可以从外部 .js 文件调用该函数。但是您需要让 ASP.NET 为您生成“click”函数:
然后在 javascript 中的其他地方只需调用 clickit()
Here's what I like to do. Since you can't really control the ID assigned by ASP.NET, you need some javascript code in the same page as the button you want clicked. you can wrap that code in a function with a known name that you can call from external .js files if need be. But you need to let ASP.NET generate the 'click' function for you:
then elsewhere in your javascript just call clickit()
它对我有用。
Its work for me.