开发者

Jquery: accessing an attribute value that has been updated since the page was loaded

开发者 https://www.devze.com 2023-03-28 10:39 出处:网络
I have 2 jquery functions: the first modifies the value of a particular attribute, and also updates the server-side database so that this value is rendered in future page loads.The second one retrieve

I have 2 jquery functions: the first modifies the value of a particular attribute, and also updates the server-side database so that this value is rendered in future page loads. The second one retrieves the value of this attribute.

If the first function is run, and the page is refreshed (so that the value is retrieved from the server and exists in the DOM on pageload), then the second one works as expected, giving the value. However, if the first function is run, but the page is not refreshed, the second function returns an empty string (the original value).

To check that something else isn't the problem, if you run the first function, and then inspect the page's source, it exactly matches the source of the refreshed page. However, jquery seems to ignore the updated value and instead retrieve that which existed before being modified.

Surely there is some way around this?

Many thanks

edit: This is how the relevent sourcecode looks when inspecting, regardless of whether you inspect it right after the value is updated using jquery, or after refreshing the page:

<input type="hidden" class="answer_key" name="answer_key" val开发者_如何学运维ue="ahVkZXZ-Y3liZXJuYXV0c3Byb2plY3RyEQsSC1JlYnVzQW5zd2VyGEAM" /> 

Why, then, does answer_key's value = "" if you don't first refresh the page?

edit2: this is driving me insane, can't figure out what the problem is. I recreated the basic functionality in very minimal form here: http://jsfiddle.net/eX78r/1/ but it works as it should, suggesting there is something peculiar going on in my code.

With that in mind, here's a link to the whole lot. If anyone can spot what the problem might be, I'd be extremely grateful - I've been stumped on this for most of the day! http://pastebin.com/enn1Eca4


Sounds like confusion about the .attr() and .prop()-methods in jQuery 1.6. Did you try .prop() in stead of .attr()?

Better to keep then synchonized at least, jQuery 1.6.0 does have a problem if you set the attr-value and retrieve the prop-value.

And in the case of value, the shorthand-function .val() (for both setting and retrieving) will work at least.

0

精彩评论

暂无评论...
验证码 换一张
取 消