使用 JavaScript 网格内联编辑,奇怪的行为

发布于 2024-09-13 03:58:18 字数 1460 浏览 2 评论 0原文

我制作了这个发票页。 有一个生成表的转发器。 发票项目描述进入用 div 标签封装的 td 标签中 像这样:

<asp:Repeater ID="Repeater1" runat="server">
           <ItemTemplate> 
            <tr>
             <td class="griditem text">
                 <div class="invoiceDescription" id='<%# "item-" + Eval("ID") %>' value='<%# Eval("ID") %>' onclick="InvoiceItemClicked(this);">
                    <%# Eval("Description") %>
                 </div>
             </td>
             <td class="gridnumb text r">
             <%# String.Format("{0:n}", Eval("Quantity"))%>
             </td>
             <td class="gridnumb text r">
             <%# String.Format("{0:c}", Eval("UnitCost"))%>
             </td>
             <td class="gridnumb text r">
             <%# String.Format("{0:c}", Eval("Total"))%>
             </td>
            </tr>
           </ItemTemplate>
          </asp:Repeater>

如果你看到第一行,我会调用 javascript onclick。函数是这样的(使用 jQuery):

function InvoiceItemClicked(elm) {
        var textbox = document.createElement('input');
        textbox.setAttribute('type', 'text');
        textbox.value = $(elm).text();
        $(elm).html(textbox);
    }

这是我第一次尝试这样做,并且在第一个镜头中就成功了。它将行转换为文本框,并在其中设置相同的文本。但问题是,当单击生成的文本框(焦点)时,其中的文本就会消失。

我也将感谢任何其他执行此内联编辑的最佳实践。然后我需要通过网络服务等更新项目。

提前致谢。

I made this invoice page.
There is a repeater that generates a table.
There are invoice item descriptions coming into td tags encapsulated with div tags
like this:

<asp:Repeater ID="Repeater1" runat="server">
           <ItemTemplate> 
            <tr>
             <td class="griditem text">
                 <div class="invoiceDescription" id='<%# "item-" + Eval("ID") %>' value='<%# Eval("ID") %>' onclick="InvoiceItemClicked(this);">
                    <%# Eval("Description") %>
                 </div>
             </td>
             <td class="gridnumb text r">
             <%# String.Format("{0:n}", Eval("Quantity"))%>
             </td>
             <td class="gridnumb text r">
             <%# String.Format("{0:c}", Eval("UnitCost"))%>
             </td>
             <td class="gridnumb text r">
             <%# String.Format("{0:c}", Eval("Total"))%>
             </td>
            </tr>
           </ItemTemplate>
          </asp:Repeater>

If you see the first line, I call a javascript onclick. The function is like this (uses jQuery):

function InvoiceItemClicked(elm) {
        var textbox = document.createElement('input');
        textbox.setAttribute('type', 'text');
        textbox.value = $(elm).text();
        $(elm).html(textbox);
    }

This is the first time I try to do sth like this, and it worked in the first shot. It transforms lines into textboxes with same text set into them. But the problem is when the generated textbox is clicked (focus), the text inside it disappears.

I will also appreciate any other best practices to do this inline editing. I need to then update the items via webservices etc..

Thanks in advance.

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

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

发布评论

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

评论(1

苍风燃霜 2024-09-20 03:58:18

是的,我的错!

function InvoiceItemClicked(elm) {
        var b = $(elm).hasClass('invoiceDescriptionEditing');
        if(!b)
        {
            var textbox = document.createElement('input');
            textbox.setAttribute('type', 'text');
            textbox.setAttribute('id', 'txt');
            $(textbox).css('width', '550px');
            textbox.value = $(elm).text();
            $(elm).html(textbox);

            $(elm).removeClass('invoiceDescriptionEditable');
            $(elm).addClass('invoiceDescriptionEditing');
        }

    }

这是正确的! :)

yep my bad!

function InvoiceItemClicked(elm) {
        var b = $(elm).hasClass('invoiceDescriptionEditing');
        if(!b)
        {
            var textbox = document.createElement('input');
            textbox.setAttribute('type', 'text');
            textbox.setAttribute('id', 'txt');
            $(textbox).css('width', '550px');
            textbox.value = $(elm).text();
            $(elm).html(textbox);

            $(elm).removeClass('invoiceDescriptionEditable');
            $(elm).addClass('invoiceDescriptionEditing');
        }

    }

this is aight! :)

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