asp.net jquery 添加带有文本框的行(克隆行)并动态下拉

发布于 2024-09-15 05:59:33 字数 2354 浏览 4 评论 0原文

当用户单击添加框属性时,需要使用 jquery 动态创建上面的行(即需要添加具有 2 个下拉列表和文本框的行)(这样就不会回发)。用户将被允许添加任意数量的属性,当他们单击复选框时,需要删除该行。如何通过 jquery 来实现这一点。

 <asp:Panel ID="pnl_BoxAttr" runat="server">
          <table>
             <tr>
                 <th>
                      Name
                  </th>
                  <th>
                      Comparision
                  </th>
                  <th>
                      Value
                  </th>
                  <th>
                      Delete
                  </th>
              </tr>
               <tr>
                   <td>
                       <asp:DropDownList ID="ddl_BoxName" runat="server">
                               <asp:ListItem Value="attr1" Selected="True"></asp:ListItem>
                               <asp:ListItem Value="attr2"></asp:ListItem>
                               <asp:ListItem Value="attr3"></asp:ListItem>
                       </asp:DropDownList>
                  </td>
                  <td>
                      <asp:DropDownList ID="ddl_BoxComparision" runat="server">
                             <asp:ListItem Value="=" Selected="true"></asp:ListItem>
                             <asp:ListItem Value=">"></asp:ListItem>
                             <asp:ListItem Value="<"></asp:ListItem>
                             <asp:ListItem Value="Like"></asp:ListItem>
                             <asp:ListItem Value="!="></asp:ListItem>
                             </asp:DropDownList>
                   </td>
                  <td>
                               <asp:TextBox ID="btn_boxval" runat="server" ></asp:TextBox>

                   </td>
                   <td>
                         <asp:CheckBox ID="chk_DeleteBoxRow" runat="server" />
                   </td>
             </tr>
             <tr>
                 <td colspan="3"> 
                     <asp:Button ID="btn_AddAttr" Text="Add Box Attribute" runat="server"/>
                 </td>
             </tr>
        </table>
        </asp:Panel>

When the user click's the add box attribute the row(i.e the row with 2 dropdown and textbox needs to be added) row with above needs to be dynamically created using jquery(so that there is no post back). User will be allowed to add as many attirbutes as he wants and when they click the check box that row needs to be deleted. How this can be achieved by jquery.

 <asp:Panel ID="pnl_BoxAttr" runat="server">
          <table>
             <tr>
                 <th>
                      Name
                  </th>
                  <th>
                      Comparision
                  </th>
                  <th>
                      Value
                  </th>
                  <th>
                      Delete
                  </th>
              </tr>
               <tr>
                   <td>
                       <asp:DropDownList ID="ddl_BoxName" runat="server">
                               <asp:ListItem Value="attr1" Selected="True"></asp:ListItem>
                               <asp:ListItem Value="attr2"></asp:ListItem>
                               <asp:ListItem Value="attr3"></asp:ListItem>
                       </asp:DropDownList>
                  </td>
                  <td>
                      <asp:DropDownList ID="ddl_BoxComparision" runat="server">
                             <asp:ListItem Value="=" Selected="true"></asp:ListItem>
                             <asp:ListItem Value=">"></asp:ListItem>
                             <asp:ListItem Value="<"></asp:ListItem>
                             <asp:ListItem Value="Like"></asp:ListItem>
                             <asp:ListItem Value="!="></asp:ListItem>
                             </asp:DropDownList>
                   </td>
                  <td>
                               <asp:TextBox ID="btn_boxval" runat="server" ></asp:TextBox>

                   </td>
                   <td>
                         <asp:CheckBox ID="chk_DeleteBoxRow" runat="server" />
                   </td>
             </tr>
             <tr>
                 <td colspan="3"> 
                     <asp:Button ID="btn_AddAttr" Text="Add Box Attribute" runat="server"/>
                 </td>
             </tr>
        </table>
        </asp:Panel>

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

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

发布评论

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

评论(2

落叶缤纷 2024-09-22 06:00:16

据我了解,您需要用户通过单击按钮插入新行(就像包含选择的行一样)?

首先,您需要为带有添加按钮的 tr 提供一个 id,假设为“addTr”。

对于插入,您可以执行以下操作:

$('#btn_AddAttr').bind(
    'click',
    function() {
        $('#addTr').before( '<tr>' + 
                                /* entre tr content here */
                            '</tr>'
                          );
    }
);

只需确保所有下拉列表和文本框都有不同的 ID。

要从表中删除行,您应该添加一个类来帮助您识别所有删除复选框,比方说“delChk”:

$('.delChk').bind(
    'click',
    function() {
        $(this).closest('<tr>').remove();
    }
);

As I understand it, you need the user to insert a new row (just like the one containing the select) by clicking a button?

First you need to give the tr with the add button an id, let's say 'addTr'.

For inserting, you could do something like this:

$('#btn_AddAttr').bind(
    'click',
    function() {
        $('#addTr').before( '<tr>' + 
                                /* entre tr content here */
                            '</tr>'
                          );
    }
);

Just be sure that all the dropdowns and textboxes have different ID's.

For removing the row from the table, you should add a class that would help you identify all the delete checkboxes, let's say 'delChk':

$('.delChk').bind(
    'click',
    function() {
        $(this).closest('<tr>').remove();
    }
);
与之呼应 2024-09-22 06:00:09

首先,这是一个工作演示,您可以在我的回答中引用它: http://jsfiddle.net/EuyB8 /

显然,该演示是纯 HTML 格式而不是 ASP.NET 格式,但我会尝试解决这些差异。

首先,由于您需要创建一些将使用 .NET 控件克隆的控件,因此我的典型方法是创建一个隐藏模板,然后可以使用该模板创建新副本。因此,我会有如下行:

<tr id="TemplateRow">
    <td>
        <asp:DropDownList ID="ddl_BoxName" runat="server">
            <asp:ListItem Value="attr1" Selected="True"></asp:ListItem>
            <asp:ListItem Value="attr2"></asp:ListItem>
            <asp:ListItem Value="attr3"></asp:ListItem>
        </asp:DropDownList>
    </td>
    <td>
        <asp:DropDownList ID="ddl_BoxComparision" runat="server">
            <asp:ListItem Value="=" Selected="true"></asp:ListItem>
            <asp:ListItem Value=">"></asp:ListItem>
            <asp:ListItem Value="<"></asp:ListItem>
            <asp:ListItem Value="Like"></asp:ListItem>
            <asp:ListItem Value="!="></asp:ListItem>
        </asp:DropDownList>
    </td>
    <td>
        <asp:TextBox ID="btn_boxval" runat="server" ></asp:TextBox>

    </td>
    <td>
        <asp:CheckBox ID="chk_DeleteBoxRow" runat="server" />
    </td>
</tr>

然后添加一个 CSS 规则,如下所示:

#TemplateRow { display:none; }

现在我们有一行可以用作添加新行的模板,并且模板的实际标记仍然可以通过以下方式生成: 。网。需要注意的是,在表格中使用此方法时,模板行需要位于要附加行的表格内部,以确保单元格具有适当的宽度。

我们需要做的最后一点标记是为表提供一个 ID,以便我们可以操作脚本中的行。我选择“BoxTable”。

现在,让我们构建我们的脚本。由于您使用的是 .NET,因此请务必记住,对于具有 runat="server" 属性的任何标记,生成的 ID 属性与您在控件 ID 中分配的 ID 属性不同场地。一个简单的解决方法是执行如下操作:

var myClientID = '<%= myServerID.ClientID() %>';

然后,服务器将客户端 ID 输出到字符串中,以便于在脚本中使用。

考虑到这一点,这是我们的脚本:

var btn_AddAttr = '<%= btn_AddAttr.ClientID() %>';

$(function() {
    //attach the a function to the click event of the "Add Box Attribute button that will add a new row
    $('#' + btn_AddAttr).click(function() {
        //clone the template row, and all events attached to the row and everything in it
        var $newRow = $('#TemplateRow').clone(true);

        //strip the IDs from everything to avoid DOM issues
        $newRow.find('*').andSelf().removeAttr('id');

        //add the cloned row to the table immediately before the last row
        $('#BoxTable tr:last').before($newRow);

        //to prevent the default behavior of submitting the form
        return false;
    });

    //attach a remove row function to all current and future instances of the "remove row" check box
    $('#DeleteBoxRow').click(function() {
        //find the closest parent row and remove it
        $(this).closest('tr').remove();
    });

    //finally, add an initial row by simulating the "Add Box Attribute" click
    $('#' + btn_AddAttr).click();
});

我认为注释非常详尽,但有必要对几点进行简要解释:

首先,当我们克隆模板行时,我们将一个布尔值传递给克隆函数。这意味着除了我们正在克隆的 DOM 元素之外,我们还应该克隆附加到这些元素的事件处理程序,并将它们附加到新的克隆。这对于“删除行”复选框的工作非常重要。

其次,当我们克隆模板行时,我们也克隆了所有元素的所有属性,包括 ID。我们不希望这样,因为它违反了 XHTML 合规性并且导致了我的问题。因此,我们剥离了所有克隆元素的 ID。

最后,由于您使用的是提交按钮,因此请务必记住在按钮的单击事件中返回 false,以避免提交表单并导致回发。

希望这能回答您的问题。最后要记住的一件事是,由于您使用 jQuery 创建所有这些行,因此服务器将没有准备好接收输入值的对象。如果您需要服务器访问该信息,则必须找出向其发送该信息的方法。

To start with, here is a working demo, which you can reference throughout my answer: http://jsfiddle.net/EuyB8/.

Obviously the demo is in plain HTML rather than ASP.NET, but I will try to address the differences.

To start, since you will want to create some of the controls that you'll be cloning using .NET controls, my typical approach is to create a hidden template, which can then be used to create new copies. As such, I would have a row like the following:

<tr id="TemplateRow">
    <td>
        <asp:DropDownList ID="ddl_BoxName" runat="server">
            <asp:ListItem Value="attr1" Selected="True"></asp:ListItem>
            <asp:ListItem Value="attr2"></asp:ListItem>
            <asp:ListItem Value="attr3"></asp:ListItem>
        </asp:DropDownList>
    </td>
    <td>
        <asp:DropDownList ID="ddl_BoxComparision" runat="server">
            <asp:ListItem Value="=" Selected="true"></asp:ListItem>
            <asp:ListItem Value=">"></asp:ListItem>
            <asp:ListItem Value="<"></asp:ListItem>
            <asp:ListItem Value="Like"></asp:ListItem>
            <asp:ListItem Value="!="></asp:ListItem>
        </asp:DropDownList>
    </td>
    <td>
        <asp:TextBox ID="btn_boxval" runat="server" ></asp:TextBox>

    </td>
    <td>
        <asp:CheckBox ID="chk_DeleteBoxRow" runat="server" />
    </td>
</tr>

I then add a CSS rule like so:

#TemplateRow { display:none; }

Now we have a row that we can use as a template for adding new rows, and the actual markup for the template can still be generated by .NET. It is important to note that when using this approach with a table, the template row needs to be inside the table to which you'll be appending the rows, in order to ensure that the cells are the appropriate width.

The last bit of markup we'll need to do is to give the table an ID, so that we can manipulate the rows in our script. I chose "BoxTable".

Now, let's construct our script. Because you are using .NET, it's important to remember that for any tag with the runat="server" attribute, the ID attribute that is generated is not the same as the one you assign in the control's ID field. An easy workaround for that is to do something like the following:

var myClientID = '<%= myServerID.ClientID() %>';

The server then outputs the client ID into a string for easy use in your script.

With that in mind, here's our script:

var btn_AddAttr = '<%= btn_AddAttr.ClientID() %>';

$(function() {
    //attach the a function to the click event of the "Add Box Attribute button that will add a new row
    $('#' + btn_AddAttr).click(function() {
        //clone the template row, and all events attached to the row and everything in it
        var $newRow = $('#TemplateRow').clone(true);

        //strip the IDs from everything to avoid DOM issues
        $newRow.find('*').andSelf().removeAttr('id');

        //add the cloned row to the table immediately before the last row
        $('#BoxTable tr:last').before($newRow);

        //to prevent the default behavior of submitting the form
        return false;
    });

    //attach a remove row function to all current and future instances of the "remove row" check box
    $('#DeleteBoxRow').click(function() {
        //find the closest parent row and remove it
        $(this).closest('tr').remove();
    });

    //finally, add an initial row by simulating the "Add Box Attribute" click
    $('#' + btn_AddAttr).click();
});

I think the comments are pretty thorough, but a brief explanation of a few points is warranted:

First, when we clone the template row, we are passing a boolean to the clone function. This says that in addition to the DOM elements we are cloning, we should also clone the event handlers attached to those elements, and attach them to the new clones. This is important in order for the "Delete Row" checkbox to work.

Second, when we clone the template row, we are also cloning all the attributes of all the elements, including the IDs. We don't want this because it violates XHTML compliance and my cause problems. Thus, we strip all the cloned elements of their IDs.

Lastly, because you are using a submit button, it's important to remember to return false in the button's click event, to avoid having it submit the form and cause a postback.

Hopefully this answers your question. One final thing to bear in mind is that since you are creating all these rows with jQuery, the server will not have objects ready to receive the values of the inputs. If you need the server to have access to that information, you'll have to figure out so method of sending it that information.

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