使用 JavaScript 从 insideHTML 文本字符串获取 selectedIndex
我所拥有的是这个 object.innerHTML ,它是这样的:
<TABLE >
<TBODY>
<TR>
<TD >
<IMG id=ctl00_Def_ctl00_ucXXXControl_gvGridName_ctl00_ctl05_imgXYZError src="etc/exclamation.png">
</TD>
<TD>
<SELECT id=ctl00_Def_ctl00_ucXXXControl_ctl00_ctl05_rcb123 name=ctl00$Def$ctl00$ucXXXControl$gvGridName$ctl00$ctl05$rcb123>
<OPTION value=0></OPTION>
<OPTION value=1>703</OPTION>
<OPTION value=3>704</OPTION>
<OPTION value=4>801</OPTION>
<OPTION value=5>802</OPTION> (etc)
</SELECT>
</TD>
</TR>
</TBODY>
</TABLE>
我需要知道如何通过 JavaScript 来使用那个 innerHTML 文本 blob。我如何获得我的选择元素的“selectedIndex”?
如果您想要这部分,则更痛苦的细节是:
我正在使用 RadGrid 控件(来自 Telerik)并使用“内联”编辑选项。所以这个网格包含 X 行,每行有 X 个单元格。细胞的内容物是问题所在。每个细胞都包含“东西”。有些包含一个简单的“输入”元素等,但我需要使用的元素包含一个表定义,该定义本身包含 1 行和 2 个单元格。一个单元格具有图像,另一个单元格具有下拉列表,即“选择”元素。
我的问题是我已经使用了 RadGrid 客户端 API 集,因此我可以深入到单元格。在这一点上,他们并没有真正提供(我能找到的)任何处理该单元格内容的方法......可能是因为内容可以是任何东西。所以我需要弄清楚如何使用这个 HTML 字符串。对于 jQuery 和 JavaScript 来说还是新手...我真的只是想将字符串转换为表对象,然后针对该对象运行 jQuery 选择器...但 JavaScript 并不能真正直接工作...据我所知迄今为止。 :(
What I have is this object.innerHTML which is this:
<TABLE >
<TBODY>
<TR>
<TD >
<IMG id=ctl00_Def_ctl00_ucXXXControl_gvGridName_ctl00_ctl05_imgXYZError src="etc/exclamation.png">
</TD>
<TD>
<SELECT id=ctl00_Def_ctl00_ucXXXControl_ctl00_ctl05_rcb123 name=ctl00$Def$ctl00$ucXXXControl$gvGridName$ctl00$ctl05$rcb123>
<OPTION value=0></OPTION>
<OPTION value=1>703</OPTION>
<OPTION value=3>704</OPTION>
<OPTION value=4>801</OPTION>
<OPTION value=5>802</OPTION> (etc)
</SELECT>
</TD>
</TR>
</TBODY>
</TABLE>
I need to know how to, via JavaScript, work with that innerHTML text blob. How would I get the "selectedIndex" of my select element?
More painful details, if you want this part:
I am working with a RadGrid control (from Telerik) and using the 'in-line' editing option. So this grid contains X rows, with each row having X cells. The contents of the cells are the issue. Each cell contains "stuff". Some contain a simple "input" element, etc, but one I need to work with contains a table definition, that itself contains 1 row with 2 cells. One cell has an image, the other cell has a dropdown list, i.e. a "select" element.
My issue is I have used the RadGrid client API set so I can drill down to the cell. At this point they don't really offer (that I can find) any way to work with the contents of that cell... probably because the contents can be anything. So I need to figure out how to work with this HTML string. Still new to jQuery and JavaScript... I really just want to cast the string as a table object and then run a jQuery selector against that object... but JavaScript doesn't really work that directly... from what I can tell so far. :(
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
将为您提供所选选项的值。
您还希望将 id 和 class 值放在 html 中的引号中。
will get you the value of the option selected.
You want to also put your id's and class values in quotes in the html.
好吧,我在帖子发布后立即想到的想法最终就是我所使用的,这也是 Shoesbox639 所建议的。我将提取所需 clientID 的代码放入辅助方法中:
因此,在我的例子中,我将传入 rcb123 作为第一个参数,将 innerHTML 字符串 blob 作为第二个值,并且该函数将返回 clientID 值。取回 clientID 后,我只是使用它进行另一个 jQuery 方法调用:
我仍在测试各种示例,并寻找除所指出的之外的任何漏洞,但就我的目的而言,这效果很好。我创建了辅助例程,因为我还操作了 insideHTML blob 中的图像。
这个想法是在网格中的每个控件旁边放置一个“错误图像”,以直观地参考错误所在的位置,但只向 errorSummary 控件添加一条错误消息,而不是我在简单嵌入时收到的 X 条重复错误消息下拉列表旁边的必填字段验证器。 (我的 BA 小组不喜欢这样......)
希望这可以帮助别人。
OK, the idea I had right after the post ended up being what I used, which is what shoebox639 suggested as well. I put the code to pull out the required clientID into a helper method:
So in my case I would pass in the rcb123 as the first parm, and the innerHTML string blob as the second value, and the function would return the clientID value. After getting the clientID back, I just do another jQuery method call using that:
I'm still testing various examples, and looking for any holes other than those noted, but for my purposes this works well. I created the helper routine because I also manipulate the image in the innerHTML blob as well.
The idea was to put an 'error image' next to each control in the grid for a visual ref to where the error was, but only add ONE error message to the errorSummary control, instead of X repeated error messages which I got when simply embedding a required field validator along side the dropdown list. (my BA group didn't like that...)
Hope this helps somebody out.