我的活动从来没有被解雇过,我也不知道为什么。这是我的语法
<asp:Button id="btnone" runat="server" visible="false" OnClick="btnone_Click" />
<script type="text/javascript">
$( document ).ready(function() {
var button = document.getElementById('btnone').click();
}
});以及我背后的C#代码
protected void btnone_Click(object sender, EventArgs e)
{
Response.Write("It was clicked through JS");
}我也尝试过使用这段代码来捕获按钮的ClientID,但是它不起作用
$( document ).ready(function () {
$('<%= btnone.ClientID %>').click();
}
});发布于 2016-02-11 13:47:06
首先,您需要将属性ClientIDMode="Static"放在按钮中,这将授予运行时相同的ID,这是您需要在Javascript中获得的。
在jQuery中,您需要这样做:
$( document ).ready(function() {
// Getting the event in a callback
$('#btnone').click(function(){
// Work here..
});
// Trigger the event
$('#btnone').click();
});
如果您想在纯JS中工作,请尝试如下所示:
$( document ).ready(function() {
// Here is the callback when the event is triggered
function getTheClick(){
// Work here..
}
// Assigning the event and it's callback
document.getElementById("btnone").addEventListener("click", getTheClick);
});
在这种情况下,您需要使用AddEventListener函数为事件分配一个函数。
发布于 2016-02-11 13:28:19
首先,您应该设置ClientIDMode=“静态”。否则,就不会有ID 'btnone‘的元素。此外,如果您设置visible="false“-它不应用样式"display:none;”。相反,按钮不会出现在页面上。您可以使用以下代码:
.aspx:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication2.WebForm1" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<script src="//code.jquery.com/jquery-1.12.0.min.js"></script>
<script type="text/javascript">
$( document ).ready(function() {
var button = document.getElementById('btnone').click();
});
</script>
<style>
#btnone
{
display:none;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Button id="btnone" runat="server" ClientIDMode="Static" OnClick="btnone_Click" Text="btn"/>
</div>
</form>
</body>
</html>.cs:
namespace WebApplication2
{
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnone_Click(object sender, EventArgs e)
{
Response.Write("It was clicked through JS");
}
}
} https://stackoverflow.com/questions/35340119
复制相似问题