如何找到动态生成的 HTML 元素?

发布于 2024-09-11 08:34:27 字数 851 浏览 11 评论 0原文

当我的 RadGrid 离开编辑模式时,我需要获取 RadComboBox 的客户端实例(或 RadComboBox 新选择的值),以便我可以将更改后的值发回服务器。

问题是,对象的客户端 ID 总是会变化。此外,RadComboBox 不是在运行时创建的 - 它们仅在用户双击 RadGrid 的特定行后创建。因此,诸如

var SundayLoc = $find("<%= FieldOpsScheduler_ctl00_ctl05_RCB_SunLocale.ClientID %>");

equals 之类的语法 FAIL。在我开始更改值之前,我能够摆脱这一点:

var SundayLoc = $find("FieldOpsScheduler_ctl00_ctl05_RCB_SunLocale");

每次都完美地工作,直到我添加了一些行分隔符对象,这些对象现在导致 ids 的“ctl05”始终根据用户放入编辑的行而变化模式。我已经尝试使用获取该对象及其值的所有方法,但无济于事。

我本来打算尝试使用正则表达式,直到阅读 bobince 的社区 wiki 答案 RegEx 匹配除 XHTML 自包含标签之外的开放标签 让我认识到,用正则表达式解析 HTML 的诱惑是邪恶的撒旦派生的工作(之后进一步研究我得出的结论是正则表达式可能对我没有帮助)。

无论如何,如果有人能以我的方式提出一些想法,我将不胜感激。先感谢您。

I need to get the client-side instance of a RadComboBox (or the newly-selected value of a RadComboBox) when my RadGrid is leaving edit mode so I can post back the changed value to the server.

The problem is, the client-side id of the object always changes. Also, the RadComboBoxes aren't created at runtime -- they're only created after the user double-clicks a specific row of my RadGrid. Therefore, syntax such as

var SundayLoc = $find("<%= FieldOpsScheduler_ctl00_ctl05_RCB_SunLocale.ClientID %>");

equals FAIL. Until I started getting changing values, I was able to get away with this:

var SundayLoc = $find("FieldOpsScheduler_ctl00_ctl05_RCB_SunLocale");

This worked perfectly every time until I added some row separator objects which now cause the "ctl05" of the ids to always change depending on what row the user puts into edit mode. I've tried using all matters of getting this object and its value, but to no avail.

I was going to try using regular expressions until after reading a community wiki answer by bobince at RegEx match open tags except XHTML self-contained tags has brought me to the realization that the temptation to parse HTML with regular expressions is the work of unholy Satan-spawn (that and after further research I've come to the conclusion that regular expressions probably won't help me).

Anyway if someone could throw some ideas my way I'd greatly appreciate it. Thank you in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

半葬歌 2024-09-18 08:34:27
  • 在服务器端创建某种类型的列表,以便在您出于某种原因渲染页面时跟踪控件 id
  • 将这些 id 写在脚本块中(将它们放入数组或其他内容中),
  • ,当您需要提交值时, 您可以使用 javascript 数组内容来引用控件。

您可能需要做更多的工作才能找到您的实际值。对于 ingragistics 的花哨控件,它们提供了一种特殊的方法来获取有关“控件”整体的信息。 rad 控件可能有类似的东西。

  • create a list of some sort on the server side to keep track of control id's
  • when you render the page for whatever reason, write those id's out in a script block (put them in an array or whatever)
  • when you need to submit your values, you can use the javascript array contents to reference the controls.

you might have to do a little more work to find your actual values. for ingragistics' fancy-pants controls they provide a special method to get information about the "control" as a whole. rad controls might have something similar.

花想c 2024-09-18 08:34:27

好吧,我最终得出的答案并不是很雄辩,但它 100% 可靠,并且全部在客户端(这很好,因为我的公司将 IE7 作为其标准 Web 浏览器 x_x)。

我能够摆脱一堆 $find 命令和 if 语句,因为我不再需要搜索特定的 HTML id。

        function SelectedIndexChanged(sender,eventArgs)
        {
            var rcbID = sender.get_id();
            var LocID = rcbID.substring(0,37) + "Locale";
            pastCombo = currentCombo;
            currentCombo = eventArgs.get_item().get_text();

            if(editedRow != null)
            {
                var Location = $find(LocID);

由于 ids 不是在服务器端生成的(据我所知,尽管我承认我没有尝试 Roatin 的解决方案,因为在看到他的答案之前我已经想到了我的解决方案+我的解决方案似乎更简单,因为我已经在做一个JavaScript 中客户端的一堆东西),我环顾四周看看我已经得到了什么,并意识到 var rcbID = sender.get_id(); 已经给我带来了一个与什么极其相似的值我已经在寻找(只是它没有说 SunLocale,而是说 SunActivity)。

所以我想,嘿,我可以 substring() 取出 rcbID 中的所有内容,然后将“Locale”附加到它,然后对其运行 $find() 命令以获得正确的 RadComboBox。事实证明它有效。不管怎样,尽管我最终找到了自己的解决方案,但我还是感谢你们研究我的问题。 :-)

Okay so the answer I ended-up coming up with isn't really eloquent, but it's 100% reliable and all on the client-side (which is good because my company has IE7 as its standard web browser x_x).

I was able to get rid of a bunch of $find commands and if-statements because I no longer have to search for the specific HTML ids.

        function SelectedIndexChanged(sender,eventArgs)
        {
            var rcbID = sender.get_id();
            var LocID = rcbID.substring(0,37) + "Locale";
            pastCombo = currentCombo;
            currentCombo = eventArgs.get_item().get_text();

            if(editedRow != null)
            {
                var Location = $find(LocID);

Since the ids weren't being generated server-side (as far as I could tell, although admittedly I didn't try Roatin's solution because I had thought of mine before I saw his answer + mine seemed simpler since I'm already doing a bunch of stuff client-side inside JavaScript), I looked around to see what I was already getting, and realized that var rcbID = sender.get_id(); was already getting me an extremely similar value to what I was already looking for (only instead of it saying SunLocale, it said SunActivity).

So I figured hey, I can substring() out everything in rcbID and then append "Locale" to it, and then run a $find() command on that in order to get me the proper RadComboBox. Turns out it works. Anyway though I do thank you guys for looking into my problem even though I ended-up finding my own solution. :-)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文