jQuery: issue with live events, a form and the bac

2019-08-07 02:22发布

问题:

I have build a quite complex widget which contains "some kind of form". It has a form tag, but I'm loading a lot of stuff in there via Ajax etc. Cannot explain it in detail, and the code is too long to paste in here.

Now, in a "live('click', function()" I use for one of the form fields, I'm writing a couple of values into hidden fields of another form. That works fine, as I can see them in the generated code. But if I leave the page and then hit the back button, the values are gone.

If I write some values into those fields outside the live click function though, they are still there when I leave the page and come back using the back button.

But I need to write the values into the hidden fields out of the live click function (I'm inserting values from fields of my form into them).

I don't know what causes this and wasn't able to find a workaround yet (even though I tried a lot).

Any ideas?

Thanks!

回答1:

Have a look at the jquery history plugin (http://plugins.jquery.com/project/history)



回答2:

Usually what happens is that browser remembers what you have entered into a form (even if you don't submit it) so that when you hit back button, it populates all the visible fields for you.

It seems it's not the case with hidden fields. There's a workaround though.

Every time one of your hidden fields is changed, you can add #part to your url (eg. www.mysite.com/users#userId,groupId,...).

When the page is loaded again (via back button for example), it will contain the #part. Parse it as a string to determine how to populate hidden fields and populate them.

Review the history plugin for jQuery to see how to read the #part.
http://plugins.jquery.com/files/jquery.history.js_0.txt



回答3:

Use CSS to hide the input instead of the input type.

<input type="text" id="foo" name="foo" style="display: none;" />

instead of

<input type="hidden" id="foo" name="foo" />

I tripped over the same issue and this seems to resolve it.