Jquery 将文本框恢复为其原始值

发布于 2024-12-14 05:29:12 字数 302 浏览 1 评论 0 原文

我有一个带有 4 个文本框的弹出表单,所有文本框在页面加载时都有默认值。

场景如下...用户已在表单中填写了详细信息,但决定不提交表单并关闭弹出窗口。我希望当用户关闭弹出窗口时文本框的值恢复为默认值。

到目前为止,这是我的代码。

$(".close").click( function() {
    $(".popup").find("input").val("Default value of textbox");
});

我希望它成为实际的默认值,而不是插入“文本框的默认值”作为值。

I have a popup form with 4 textboxes, all of which have default values when the page loads.

Here's the scenario... The user has filled in the form with their details but decides not to submit the form and closes the popup. I want the values of the textboxes to revert to their default values when the user close the popup.

Here's my code so far.

$(".close").click( function() {
    $(".popup").find("input").val("Default value of textbox");
});

Rather than inserting "Default value of textbox" as the value, I want it to be the actual default value.

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

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

发布评论

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

评论(3

七分※倦醒 2024-12-21 05:29:12

您可以将默认值放在元素本身的“数据属性”中...我将创建一个小提琴来显示这一点... 如 jsFiddle 中所示,

我还应该注意,您可以将数据属性放在服务器上的元素上...我只是展示如何从客户端完成这一切...

You could put the default values in a 'data attribute' on the elements themselves...I'll create a fiddle to show this... As seen in this jsFiddle

I should also note that you can put the data attribute on the elements from the server...I'm just showing how you could do it all from the client...

玩物 2024-12-21 05:29:12
                // First store all the default values
                var def_vals = [];

                jQuery('#popup input[type="text"]').each(

                    function () {

                        def_vals[jQuery(this).attr('name')] = jQuery(this).val();

                    }
                );

                alert(def_vals['test']);

            // Restore defaults on close



            jQuery('#popup .closeButton').click( function () {

                jQuery('#popup input[type=text]').each(

                    function() {

                        jQuery(this).val(def_vals[jQuery(this).attr('name')]);

                    }

                );

            });
                // First store all the default values
                var def_vals = [];

                jQuery('#popup input[type="text"]').each(

                    function () {

                        def_vals[jQuery(this).attr('name')] = jQuery(this).val();

                    }
                );

                alert(def_vals['test']);

            // Restore defaults on close



            jQuery('#popup .closeButton').click( function () {

                jQuery('#popup input[type=text]').each(

                    function() {

                        jQuery(this).val(def_vals[jQuery(this).attr('name')]);

                    }

                );

            });
流星番茄 2024-12-21 05:29:12

通过关闭的方式

// add to the place that opens the popup
$(".popup input").one(function(){
   var value = $(this).val();
   var that = this;
   $(".close").one(function(){
      $(that).val(value);
   });
});

By way of closure

// add to the place that opens the popup
$(".popup input").one(function(){
   var value = $(this).val();
   var that = this;
   $(".close").one(function(){
      $(that).val(value);
   });
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文