如何动态地找到使用JavaScript控件的ClientID? [英] How to dynamically find ClientIDs of controls using javascript?

查看:133
本文介绍了如何动态地找到使用JavaScript控件的ClientID?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我工作的一个JavaScript函数,该函数在三个控件的名称,然后找到它们在页面上。有五套这些控件。为简单起见,我想用同样的功能,并通过在一套控制的名字,然后让函数通过动态clientID的找到控制。有没有办法做到这一点?

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?

这是我迄今为止...

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>&nbsp';
                ctrl.pasteHtml(strSpan);
            }
        }

这是不行的,但它说明了什么,我试图做的。

This doesn't work, but it illustrates what I'm trying to do.

你如何使用动态发现控制的ClientID的JavaScript?

推荐答案

&LT;%=%&GT; 是服务器端,而不是客户端code ,所以不是...

<%= %> is server-side, not client-side code, so instead of...

if (OnSubj) ctrl = $find("<%=" + subjCtrl + ".ClientID%>");
if (OnBody) ctrl = $find("<%=" + bodyCtrl + ".ClientID%>");

您应该有类似...

if (OnSubj) ctrl = $find("<%=subjCtrl.ClientID%>");
if (OnBody) ctrl = $find("<%=bodyCtrl.ClientID%>");

其中, subjCtrl bodyCtrl 是实际的服务器端控件对象。

Where subjCtrl and bodyCtrl are actual server-side control objects.

你可以让你的JavaScript工作的唯一办法是,如果你调用的函数这样的事情...

The only way you could get your JavaScript to work was if you CALLED the function something like this...

InsertKeyword("my keyword", "<%=subjCtrl.ClientID%>", "<%=bodyCtrl.ClientID%>");

再有你的JavaScript类似...

And then had your JavaScript something like...

function InsertKeyword(keywordCtrl, subjCtrl, bodyCtrl) {
  var ctrl;
  if (OnSubj) ctrl = $find(subjCtrl);
  if (OnBody) ctrl = $find(bodyCtrl);

更新

根据由OP的评论,这是不可能使用&LT;%=%&GT; 声明时语法的OnClientClick 上通过加价的服务器端控件的属性

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.

以下将无法正常工作,而&LT;%= myCtrl.ClientID%GT; 将呈现为准确,当发送到浏览器...

The following will not work, and the <%=myCtrl.ClientID%> will be rendered as exactly that when sent to the browser...

<asp:Button runat="server" ID="test" OnClientClick="myFnc('<%=myCtrl.ClientID%>')"/>

相反,你需要通过以下方法之一通过code-背后设置属性(C#假设)...

Instead you need to set the attribute via the code-behind (C# assumed) via one of these methods...

test.OnClientClick = "myFnc('" + myCtrl.ClientID + "');";
test.OnClientClick = string.Format("myFnc('{0}');", myCtrl.ClientID);

这篇关于如何动态地找到使用JavaScript控件的ClientID?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆