javascript 如何使用javascript动态查找控件的ClientID?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11453245/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to dynamically find ClientIDs of controls using javascript?
提问by ks78
I'm working on a javascript function which takes in the names of three controls, then find them on the page. There are five sets of these controls. For simplicity, I would like to use the same function and pass in the set of control names, then have the function dynamically find the controls by clientID. Is there a way to do this?
我正在开发一个 javascript 函数,它接受三个控件的名称,然后在页面上找到它们。这些控件有五组。为简单起见,我想使用相同的函数并传入控件名称集,然后让函数通过clientID动态查找控件。有没有办法做到这一点?
Here's what I have so far...
这是我到目前为止...
function InsertKeyword(keywordCtrl, subjCtrl, bodyCtrl) {
var ctrl;
if (OnSubj) ctrl = $find("<%=" + subjCtrl + ".ClientID%>");
if (OnBody) ctrl = $find("<%=" + bodyCtrl + ".ClientID%>");
if (OnSubj == 1 || OnBody == 1) {
var selectedIndex = document.getElementById(keywordCtrl).selectedIndex;
var selectedText = document.getElementById(keywordCtrl).options[selectedIndex].text;
var strSpan = '<u>' + selectedText + '</u> ';
ctrl.pasteHtml(strSpan);
}
}
This doesn't work, but it illustrates what I'm trying to do.
这不起作用,但它说明了我正在尝试做的事情。
How do you dynamically find the ClientIDs of controls using javascript?
如何使用javascript动态查找控件的ClientID?
采纳答案by freefaller
<%= %>
is server-side, not client-side code, so instead of...
<%= %>
是服务器端,而不是客户端代码,所以不是......
if (OnSubj) ctrl = $find("<%=" + subjCtrl + ".ClientID%>");
if (OnBody) ctrl = $find("<%=" + bodyCtrl + ".ClientID%>");
You should have something like...
你应该有类似...
if (OnSubj) ctrl = $find("<%=subjCtrl.ClientID%>");
if (OnBody) ctrl = $find("<%=bodyCtrl.ClientID%>");
Where subjCtrl
and bodyCtrl
are actual server-side control objects.
其中subjCtrl
和bodyCtrl
是实际的服务器端控制对象。
The only way you could get your JavaScript to work was if you CALLED the function something like this...
让 JavaScript 工作的唯一方法是,如果你像这样调用函数......
InsertKeyword("my keyword", "<%=subjCtrl.ClientID%>", "<%=bodyCtrl.ClientID%>");
And then had your JavaScript something like...
然后让你的 JavaScript 像......
function InsertKeyword(keywordCtrl, subjCtrl, bodyCtrl) {
var ctrl;
if (OnSubj) ctrl = $find(subjCtrl);
if (OnBody) ctrl = $find(bodyCtrl);
UPDATE
更新
Based on the comment by the OP, it is not possible to use the <%= %>
syntax when declaring OnClientClick
attribute on a server-side control via the mark-up.
根据 OP 的评论,在通过 mark-up在服务器端控件上<%= %>
声明OnClientClick
属性时无法使用该语法。
The following will not work, and the <%=myCtrl.ClientID%>
will be rendered as exactly that when sent to the browser...
以下将不起作用,并且<%=myCtrl.ClientID%>
将按照发送到浏览器时的方式呈现...
<asp:Button runat="server" ID="test" OnClientClick="myFnc('<%=myCtrl.ClientID%>')"/>
Instead you need to set the attribute via the code-behind (C# assumed) via one of these methods...
相反,您需要通过以下方法之一通过代码隐藏(假设为 C#)设置属性...
test.OnClientClick = "myFnc('" + myCtrl.ClientID + "');";
test.OnClientClick = string.Format("myFnc('{0}');", myCtrl.ClientID);
回答by Waqar Janjua
If I understand you question then I think you want the control's ID in javascript. If you are using Asp.Net 4, then set the controls ClinetIDMode = Static
. Now in javascript enter the same control id. FOr example if you have a control with ID myControl
then in javascript you can get it as
如果我理解你的问题,那么我认为你想要 javascript 中的控件 ID。如果您使用的是 Asp.Net 4,则设置控件ClinetIDMode = Static
。现在在 javascript 中输入相同的控件 ID。例如,如果您有一个带有 ID 的控件,myControl
那么在 javascript 中,您可以将其作为
var cID = document.getElementByID('myControl');
回答by Amiram Korach
You can't do that:
你不能这样做:
"<%=" + subjCtrl + ".ClientID%>"
"<%=" + subjCtrl + ".ClientID%>"
You need:
你需要:
"<%= codeBehindControlID.ClientID %>"
"<%= codeBehindControlID.ClientID %>"
回答by webnoob
How about using CSS selectors to get control matching on the last part of the ASP.NET clientID, like so:
如何使用 CSS 选择器在 ASP.NET clientID 的最后部分获得控件匹配,如下所示:
$.find('input[name$='+subjCtrl+']:checked');
Not tested but it should be something like that.
没有测试,但它应该是这样的。
EDIT:I don't know what controls you are using but this is an example. Can provide a more detailed one if you post your ASP.NET markup as well.
编辑:我不知道你在使用什么控件,但这是一个例子。如果您也发布 ASP.NET 标记,可以提供更详细的标记。