Passing ASP.NET client IDs in to a javascript function(将 ASP.NET 客户端 ID 传递给 javascript 函数)
问题描述
我需要在 ASP.net 控件事件的 onblur 事件中将客户端 ID 传递给 Javascript 函数,如下所示:
I need to pass a client ID to a Javascript function in the onblur event of an ASP.net control event like this:
OnBlur="javascript:setBackground(this, '<%= txtClientName.ClientID %>')"
这是我的 Javascript 函数:
Here is my Javascript function:
function setBackground(sender, controlID) {
sender.style.backgroundColor = "#ffffff";
var nextElement = document.getElementById(controlID);
if ((nextElement.value == '' || nextElement.value == 'Select') && tab == true) {
nextElement.style.backgroundColor = "#f7C059"
tab = false;
}
}
问题在于,客户端 ID 是按字面意思作为<%= txtClientName.ClientID %>"而不是实际值传入的.所以,调用 document.getElementById(controlID);不起作用.
The problem is that the client ID gets passed in literally as '<%= txtClientName.ClientID %>' instead of the actual value. So, calling document.getElementById(controlID); doesn't work.
如何获取实际的客户端 ID 并将其传递给我的 Javascript 函数?
How can I get the actual client ID and pass it to my Javascript function?
推荐答案
您可以将 asp.net 控件更改为标准 html 元素(即,不使用 runat="server")
You could either change the asp.net control to standard html element (i.e., without runat="server")
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<input type="text" id="ClientText1" onblur="javascript:alert('<%= TextBox1.ClientID %>')" />
或查看此堆栈溢出答案:
or see this Stack Overflow answer:
在 asp 中分配声明值的问题:超链接.错误:这不是 scriptlet.将输出为纯文本
或者使用 jquery
or use jquery
<script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.5.2/jquery.min.js"> </script>
<script type="text/javascript">
$(document).ready(function () {
$("#text2").blur(function(){alert('<%= TextBox1.ClientID %>')});
});
</script>
这篇关于将 ASP.NET 客户端 ID 传递给 javascript 函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:将 ASP.NET 客户端 ID 传递给 javascript 函数
基础教程推荐
- .NET SerialPort DataReceived 事件未触发 2022-01-01
- Moq It.Is<>不匹配 2022-01-01
- C# 从 List<List<int>> 中删除重 2022-01-01
- 如何使用 .Net 检查 Active Directory 服务器是否已启动并正在运行? 2022-01-01
- 禁止输入少量字符,例如'<'、'&a 2022-01-01
- WPF 模态进度窗口 2022-01-01
- 如果有人提交恶意软件Nuget包怎么办? 2022-01-01
- 我应该在后面的代码中直接使用 Linq To SQL 还是使 2022-01-01
- 当值可以是对象或空数组时反序列化 JSON 2022-01-01
- Azure Functions:CosmosDBTrigger 未在 Visual Studio 中触发 2022-01-01
