asp.net - 如何使用 javascript 从 ASP.NET 按钮获取 url 地址
问题描述
我想使用 javascript 从 ASP.NET 按钮获取 PostBackUrl 值,但这似乎是不可能的,因为 onclick 属性具有嵌入 url 地址的 javascript 代码。
这是我在 Chrome F12 调试器中看到的:
<input type="submit" name="btnGroceryShelve" value="Grocery Shelve"
onclick="javascript:WebForm_DoPostBackWithOptions(
new WebForm_PostBackOptions(
"btnGroceryShelve", "", false, "",
"https://www.grocerystore.com/", false, false))"
id="btnGroceryShelve">
这是asp.net代码:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs"
Inherits="TestApp.WebForm1" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Test</title>
<script src="Scripts/jquery-3.6.0.min.js"></script>
<script type="text/javascript">
function GetAddress() {
var address = $("#btnGroceryShelve").attr('onclick');
alert("this address is " + address);
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<button type="button" onclick="GetAddress()">Get Address</button>
<asp:Button ID="btnGroceryShelve" ClientIDMode="Static"
runat="server" Text="Grocery Shelve"
PostBackUrl="https://www.grocerystore.com/"/>
</div>
</form>
</body>
</html>
是否可以在 javascript 中提取 PostBackUrl 值?
解决方案
添加ClientIDMode="Static"
到您<asp:Button>
的标记中,它将在您的标记中包含 id 'btnGroceryShelve'。
推荐阅读
- javascript - 在页面上重新加载特定的 div 而不在 rails 中刷新页面
- python-2.7 - 如何在 python 2.7 中使用 selenium 模块打开 Web 浏览器?
- python - 如何将自定义标签/过滤器添加到现有的 Django 应用程序?
- python - 从python中的网页中提取所有表
- kubernetes - 当节点死亡/重新启动并具有 PersistentVolume 时的 StatefulSet 行为
- svg - SVG 文本路径中的颠倒文本
- java - 如何使在 try 块中声明的最终变量在 catch / finally 块中使用?
- javascript - 如何在拦截响应时更新 axios 实例?
- javascript - 我可以以某种方式在 webview 中包含一个平面列表吗?
- unity3d - 如何在 Unity 中计算鼠标指针在某个 3D 对象上的位置?