通过Jquery获取转发器内的控件ID

发布于 2024-11-15 23:54:02 字数 369 浏览 3 评论 0原文

我有一些条目我想对它们发表评论。所以我创建了一个带有文本框和 里面有按钮。如何获取特定行的按钮和文本框的 id jQuery?

<ASP:REPEATER runat="server">
<ItemTemplate>

...
// Entries: bind data from DB

<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>

<asp:Button ID="Button1" runat="server" />  

</ItemTemplate>
</ASP:REPEATER>

谢谢 。

I have Entries I want to comment on them . So I created a Repeater with a TextBox and a
Button inside it . How can I get the id of the button and textbox for specific row by
Jquery ?

<ASP:REPEATER runat="server">
<ItemTemplate>

...
// Entries: bind data from DB

<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>

<asp:Button ID="Button1" runat="server" />  

</ItemTemplate>
</ASP:REPEATER>

Thanks .

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

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

发布评论

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

评论(2

茶底世界 2024-11-22 23:54:02

你的大问题是你的asp转发器控件将生成像这样的id ct100_repeater_txt1 这不会帮助你获得与其相应的数据库行关联的注释

我发现完成此任务的常用方法(我认为这是一种非常标准的方法)是将元素 id 分配给数据库中的行 id,或者如果您无法控制 id,则创建自定义属性。

我使用的伪代码看起来像这样:(我将尝试使其与语言/平台无关,以便您可以学习概念,而不是仅学习语言)

<repeater template>
    <textbox id="txt1" dbid='<% eval database id>'/>
    <button id="btn1"  dbid='<% eval database id>'/>
</repeater template>

应该生成如下所示的代码:

<input type="textbox" value="" id="ct100_txt1" dbid="14" />
<input type="button value="submit" id="ct100_btn1" dbid="14" />

<input type="textbox" value="" id="ct100_txt2" dbid="12" />
<input type="button value="submit" id="ct100_btn2" dbid="12" />

<input type="textbox" value="" id="ct100_txt3" dbid="39" />
<input type="button value="submit" id="ct100_btn3" dbid="39" />

然后以您想要的任何语言你可以从这个属性中读到:

Button.Click{
   get attribute dbid of button clicked;
   save text of textbox of same dbid to a database;
}

欢呼吧!希望这对你有用

编辑

回应评论“id 在哪里保存 dbid?!”我假设在数据库中:)如何保存它?有很多方法。您可以尝试使用 jquery:

创建一个 JavaScript 函数来保存带有参数的答案。当您将 dbid 绑定到控件时,将其绑定到 onclick 函数并将该 id 传递给该函数。然后,该函数获取 dbid 匹配的文本,并将评论和 id 保存到数据库中。

<textbox dbid="14" />
<input onclick="doStuff(14)" />
<script>
    function doStuff(var id){
        var comment = $('textbox[dbid=id]').value();
        ajax(your url and arguments);
    };
</script>

Your big problem is that your asp repeater control is going to generate ids like this ct100_repeater_txt1 which isn't going to help you get that comment associated with its corresponding db row

The common way I have found to accomplish this (which is a pretty standard way I believe) is to assign the elements id with the row id from the database, or if you have no control over the id, to create a custom attribute.

Pseudo code I have used looks something like this: (I'll try and make it language/platform agnostic so you can learn the concept, as opposed to only the language)

<repeater template>
    <textbox id="txt1" dbid='<% eval database id>'/>
    <button id="btn1"  dbid='<% eval database id>'/>
</repeater template>

that should generate code that looks like:

<input type="textbox" value="" id="ct100_txt1" dbid="14" />
<input type="button value="submit" id="ct100_btn1" dbid="14" />

<input type="textbox" value="" id="ct100_txt2" dbid="12" />
<input type="button value="submit" id="ct100_btn2" dbid="12" />

<input type="textbox" value="" id="ct100_txt3" dbid="39" />
<input type="button value="submit" id="ct100_btn3" dbid="39" />

Then in whatever language you want you can read from that attribute:

Button.Click{
   get attribute dbid of button clicked;
   save text of textbox of same dbid to a database;
}

Many cheers! Hope that works for ya

EDIT

In response the the comment "where do id save the dbid?!" I'm assuming in a database :) How should you save it? There are lots of ways. Here's one you could try with jquery:

create a javascript function to save answers that takes a parameter. when you bind the dbid to the control, bind it into an onclick function and pass that id to the function. The function then gets the text where the dbid matches and saves the comment and id to a database.

<textbox dbid="14" />
<input onclick="doStuff(14)" />
<script>
    function doStuff(var id){
        var comment = $('textbox[dbid=id]').value();
        ajax(your url and arguments);
    };
</script>
第几種人 2024-11-22 23:54:02

没有更多信息,这是我可以为您提供的最好信息:

对于像这样的 html(这就是 ASP.NET 将如何呈现按钮/文本框):

<div id="row1"><button type="button">Click Me!</button><input type="text" name="tb_info" /></div>
<div id="row2"><button type="button">Click Me again!</button><input type="text" name="tb_info" /></div>

您可以使用以下命令选择特定行的按钮和文本框 :以下 jQuery:

$("#row1 > button, #row1 > input")

Without more information this is the best I can offer you:

for html like this (which is how asp.net will render your button/textbox):

<div id="row1"><button type="button">Click Me!</button><input type="text" name="tb_info" /></div>
<div id="row2"><button type="button">Click Me again!</button><input type="text" name="tb_info" /></div>

You could select the button and textbox for specific row by using the following jQuery:

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