在ajax提交后,如何更新表单字段的默认值(不是表单重置) [英] How can I update a default value for a form field after an ajax submission (not a form reset)

查看:351
本文介绍了在ajax提交后,如何更新表单字段的默认值(不是表单重置)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用jQuery通过AJAX提交表单。这将更新数据库记录,然后返回响应成功或失败。我所遇到的问题并不重要,但为方便起见,更为方便。



当重新设置表单时,会从页面加载时将所有字段重置为默认值(为了简单。)这不是我想要的行为。我想改变这个的原因是因为如果我提交表单,然后我决定再次更改值,那么我决定要将表单重置为我最后提交的值,它会重新设置为初始默认值,而不是我的新值最近提交并保存在数据库中的值。



要尝试更好地解释这一点,我将使用用户用户名的表单字段显示我的过程。

 用户名:SomeDefaultUserNameFetched 

然后将用户名更改为Foo。

 用户名:Foo 
pre>

我通过AJAX提交表单并获得成功回复。
然后我决定也许我不喜欢Foo,而且更喜欢酒吧。

 用户名:Bar 

然后我决定我不喜欢Bar和Foo是好的,所以我决定重置表单。
重置表单将给我:

 用户名:SomeDefaultUserNameFetched 
/ pre>

如您所见,表单重置不会保存我最近提交的表单数据。所以我的问题是,如果这是可能的话,更新表单默认值,以便当我重置时,表单将重新设置我的最新提交的字段。



谢谢,我真的希望有人可以推动我正确的方向,或者让我知道,如果这是可能的。



更新:
如果可能,我想将其保存到DOM中。首先存储的默认值在哪里?我知道我可以修改我的自定义表单重置以处理设置表单元素值,但是如果有办法更新DOM,我试图不必造成这种开销。



感谢您的帮助

解决方案

找到解决方案:
我发现这个:作为我的解决方案,a href =http://www.w3schools.com/jsref/prop_text_defaultvalue.asp =nofollow> http://www.w3schools.com/jsref/prop_text_defaultvalue.asp 。通过循环我的提交内容,我可以更改默认值。


I am using jQuery to submit my form via AJAX. This updates a database record and then returns a response for success or failure. The issue I am having is not critical but more for convenience.

When resetting a form it resets all the fields to their default values from when the page loads (for sake of simplicity.) This is not the behavior I want. The reason I want to change this is because if I submit the form and I then decide to change the value again then I decide I want to reset the form back to my last submitted values, it resets back to the initial defaults and not my new values that were recently submitted and saved in the database.

To try and better explain this I will show my process with the form field for a user's username.

Username: SomeDefaultUserNameFetched

I then change the username to Foo.

Username: Foo

I submit the form via AJAX and get a success response. I then decide maybe I don't like Foo and would prefer Bar.

Username: Bar

Then I decide I don't like Bar and Foo was just fine so I decide to reset the form. Resetting the form will give me:

Username: SomeDefaultUserNameFetched

As you can see the form reset is not saving my recently submitted form data. So my question is how can I, if this is even possible, update the form defaults so that when I hit reset the form will reset the fields with my latest submission.

Thanks and I really hope someone can push me in the right direction or let me know if this is even possible.

UPDATE: I would like to be able save this into the DOM if possible. Where are the default values stored at in the first place? I know I could modify my custom form reset to handle setting the form element values but I was trying not to have to cause this overhead if there was a way to update the DOM.

Thanks for the assistance

解决方案

Found Solution: I found this: http://www.w3schools.com/jsref/prop_text_defaultvalue.asp as my solution. By looping through my submission content I can update the default values as they are changed.

这篇关于在ajax提交后,如何更新表单字段的默认值(不是表单重置)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆