使用 JavaScript 访问中继器值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8342583/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 03:07:53  来源:igfitidea点击:

Access Repeater values using JavaScript

javascript.netrepeater

提问by Dipa Ahuja

i searched a lot on NET, to get the solution, but i could not find

我在 NET 上搜索了很多以获得解决方案,但我找不到

Can anyone tell me how to access the label and textbox values of repeater control inside using the javascript ?

谁能告诉我如何使用 javascript 访问内部转发器控件的标签和文本框值?

This is my code

这是我的代码

 <asp:Repeater ID="Repeater1" runat="server" DataSourceID="SqlDataSource1">
    <ItemTemplate>
        <table id="t1" width="200px:" style="background-color: skyblue" runat="server">
            <tr>
                <td>
                    <asp:TextBox ID="TextBox3" Text='<%#DataBinder.Eval(Container.DataItem, "empid")%>'
                        runat="server" />
                    <asp:CheckBox ID="CheckBox1" runat="server" />
                    <asp:Label ID="Label1" runat="server" Text='<%#DataBinder.Eval(Container.DataItem, "empid")%>'></asp:Label>
                    <asp:Label ID="lblname" runat="server" Text='<%#DataBinder.Eval(Container.DataItem, "ename")%>'></asp:Label>
                    <br />
                    <br />
                </td>
            </tr>
        </table>
    </ItemTemplate>
</asp:Repeater>

Now i want to access the label, textbox of repeater using javascript

现在我想使用 javascript 访问标签、转发器的文本框

@Diodeus

@Diodeus

I tried your code

我试过你的代码

function submitAll() {
        var thisLabel = $('.myLabel').eq(0);
        alert(thisLabel);
    }

But i got the result in alert as

但我在警报中得到了结果

[object Object]

[对象对象]

and @deostroll

和@deostroll

I tried your code this way

我这样试过你的代码

But not getting anything

但什么也得不到

    function GetData() {
        var arrTables = document.getElementById('myDiv').getElementsByTagName('table');
        var tbl = arrTables[0];
        var td = tbl.childNodes[0].childNodes[0].childNodes[0];
        var txt = td.childNodes[0];
        alert(txt.value);        
    }

回答by Diodeus - James MacFarlane

<asp:Label ID="Label1" runat="server" Text='<%#DataBinder.Eval(Container.DataItem, "empid")%>'></asp:Label>

IDs must be unique, so you can't apply the same ID to all of the labels in your repeater. Use CSS class names instead.

ID 必须是唯一的,因此您不能将相同的 ID 应用于中继器中的所有标签。改用 CSS 类名。

<asp:Label CssClass="myLabel" runat="server" Text='<%#DataBinder.Eval(Container.DataItem, "empid")%>'></asp:Label>

Since jQuery comes with .NET you can use it instead of plain JavaScript to access these elements more easily.

由于 jQuery 与 .NET 一起提供,您可以使用它代替普通的 JavaScript 来更轻松地访问这些元素。

var thisLabel = $('.myLabel').eq(0)where 0 is the index of the element since there can be many.

var thisLabel = $('.myLabel').eq(0)其中 0 是元素的索引,因为可以有很多。

回答by deostroll

Wrap the repeater in a div with some id, say myDiv.

用一些 id 将转发器包装在一个 div 中,比如 myDiv。

<div id="myDiv">
<!-- your repeater code -->
<asp:Repeater ID="Repeater1" runat="server"...>
<ItemTemplate>
<table>...</table>
</ItemTemplate>
</asp:Repeater>
</div>

Do a

做一个

var arrTables = document.getElementById('myDiv').getElementsByTagName('table');

Now arrTablesis just array of all table elements inside the div. Find the ordinal of the desired table: For e.g. sake I am taking first table.

现在arrTables只是 div 中所有表格元素的数组。找到所需表的序数:例如,我正在拿第一张表。

var tbl = arrTables[0];

Find the corresponding tdof the table element:

找到table元素对应的td

var td = tbl.childNodes[0].childNodes[0].childNodes[0];

The above may vary based on how your browser loads the DOM and stuff. Hence I say you debug and find out for your self.

以上内容可能因浏览器加载 DOM 和内容的方式而异。因此我说你自己调试并找出答案。

Once you get the tdreference:

获得td参考后:

var txt = td.childNodes[0];

This will give the textbox. txt.nextSiblingwill give the label...and so on.

这将给出文本框。txt.nextSibling会给标签...等等。