Jquery 将表单输入存储在重写为 doc 的变量中,它们具有原始值

发布于 2024-10-06 01:21:01 字数 275 浏览 1 评论 0原文

我正在使用 jQuery 为印刷店构建模板表单。用户可以填写表格并单击“预览”按钮。此时,我将表单存储在变量中,并使用 ReplaceWith() 重写文本输入,以将输入从表格单元格中交换出来,并将其替换为其值。 此时,我会显示一个“编辑更多”按钮,以防他们想重新考虑他们的输入。 所以,...然后我将存储变量中的 html 写回到表单中,但令人惊讶的是,文本输入值是用户输入之前的原始默认值。 这是一个相当复杂的项目。我讨厌发布代码,因为我觉得这只会使讨论变得复杂。我希望有人已经有过这种经历,所以知道发生了什么! :-) 谢谢大家。

I am working with jQuery building a Template form for a print shop. Users can fill out the form and click a "Preview" button. At this point, I store the form in a variable and rewrite the text inputs using replaceWith() to swap the input out of the table cell and replace it with its' value.
At this point I show an "Edit More" button in case they want to reconsider their input.
So,... I then write the html in the stored variable back into the form, but surprise, the text input values are the original defaults from before user entry.
This is a pretty complex project. I hate to post code because I feel it will just complicate the discussion. I hope someone has already had this experience so knows just what is up! :-)
Thanks all.

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

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

发布评论

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

评论(1

少钕鈤記 2024-10-13 01:21:02

如果我正确理解您的要求,我会使用 jQuery 的 .detach() 重写您的一些代码 方法。它允许您将通常要删除的元素存储到变量中,以便稍后可以将它们重新插入回 DOM 中。

您仍然可以像常规 jQuery 对象一样与分离的元素进行交互,但您不会在页面上看到它们。

这是一个示例:

<script>
$(function(){

    var detachedInput = '';
    var detachedBtn = '';

    $('#preview').click(function(){
        // store elements for later use
        detachedInput = $('#input').detach();
        detachedBtn = $(this).detach();

        // whatever you're trying to do with the output
        //...this is just an example, and not a very good one at that
        $('body').html($(detachedInput).val());
        $('<button>Edit</button>').appendTo('body')
            .click(function(){
                $('body').empty()
                    .append(detachedInput)
                    .append(detachedBtn);
            });
    });

});
</script>

<input id='input' />
<button id='preview'>Preview</button>

作为旁注,发布一些代码通常会有所帮助,因为它可以让每个人更好地了解您要做什么。

If I understand your requirements correctly, I would rewrite some of your code using jQuery's .detach() method. It lets you store the elements you would normally be removing into a variable so they can then be reinserted back into the DOM at a later point in time.

You will still be able to interact with the detached elements as regular jQuery objects, but you won't see them on the page.

Here's an example:

<script>
$(function(){

    var detachedInput = '';
    var detachedBtn = '';

    $('#preview').click(function(){
        // store elements for later use
        detachedInput = $('#input').detach();
        detachedBtn = $(this).detach();

        // whatever you're trying to do with the output
        //...this is just an example, and not a very good one at that
        $('body').html($(detachedInput).val());
        $('<button>Edit</button>').appendTo('body')
            .click(function(){
                $('body').empty()
                    .append(detachedInput)
                    .append(detachedBtn);
            });
    });

});
</script>

<input id='input' />
<button id='preview'>Preview</button>

As a side note, posting some code usually does help as it gives everyone a better idea of what you're trying to do.

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