简体   繁体   English

$('#textboxId').val() 返回旧值(浏览器缓存问题)

[英]$('#textboxId').val() returning old value (browser cache issue)

I can't figure this out despite spending hours.尽管花了几个小时我还是想不通。 I have a simple dynamically created page (a vbhtml that is loaded using a controller) with two text boxes.我有一个简单的动态创建的页面(一个使用控制器加载的 vbhtml),带有两个文本框。

There is a save button on the page.页面上有一个保存按钮。 This button invokes an in-page Javascript function.此按钮调用页面内 Javascript 函数。 In the function I have these lines:在函数中,我有以下几行:

function saveData()
{
var lDescr = $('#FundNote').val();  // FundNote is the id of a textbox
var DocUrl = $('#DocUrl').val();  // DocUrl is the id of another textbox
alert(lDescr);
alert(DocUrl);
  //ajax call to a controller
}

The issue is that every time this page displayed, these variables continue returning the same values as entered the very first time.问题是每次显示此页面时,这些变量都会继续返回与第一次输入的值相同的值。 If I clean browser cache, the newly entered values are returned but then it keeps returning the same values until the browser cache is cleaned again.如果我清理浏览器缓存,则返回新输入的值,但它会一直返回相同的值,直到再次清理浏览器缓存。 Every time this page is displayed, the text boxes are empty and user enters values, so the page itself is not being holding up the values.每次显示此页面时,文本框都是空的,用户输入值,因此页面本身不会保留值。

Any pointers?任何指针?

UPDATE更新

After spending days, this is how I fixed it (temporarily).花了几天后,这就是我修复它的方式(暂时)。

It appears that even though the controller is sending the updated html and was displayed ok on the browser (the html includes a dropdown box, which is dynamically filled with different values. Everytime the values are different but in javascript, old values are being returned (for example, old value of the select option, the value did not even included in the current rendering of the html). Still no idea why this is happening. There are other similar pages in the solution and they work fine.).看来,即使控制器正在发送更新的 html 并且在浏览器上显示正常(html 包括一个下拉框,它动态填充不同的值。每次值不同但在 javascript 中,旧值正在返回(例如,选择选项的旧值,该值甚至没有包含在 html 的当前呈现中)。仍然不知道为什么会发生这种情况。解决方案中还有其他类似的页面,它们工作正常。)。

Anyway, what I did (for now to make it working), I added a RandonNo() property in the class that dynamically creates this html (via a controller and a vbhtml file).无论如何,我所做的(现在使其工作),我在动态创建此 html 的类中添加了 RandonNo() 属性(通过控制器和 vbhtml 文件)。 Then in the vbhtml, I appended this randon number to all html component ids.然后在 vbhtml 中,我将此随机数附加到所有 html 组件 ID。 I also passed the same number to the javascript " Save() " function like this OnClick="Save('@Model.RandonNo') . In the Save method, then I changed $('#idOption') to $('#idOption-'+ randomno) . It seems to be working because the ids are different everytime.我还将相同的数字传递给了 javascript“ Save() ”函数,就像这样OnClick="Save('@Model.RandonNo') 。在 Save 方法中,然后我将$('#idOption') to $('#idOption-'+ randomno) 。它似乎有效,因为ids每次都不同。

I know this is an old question, but I felt it needed a solution, as jQuery is still doing this.我知道这是一个老问题,但我觉得它需要一个解决方案,因为 jQuery 仍在这样做。 I had been having a very similar issue, but was able to finally find a work-around.我一直有一个非常相似的问题,但最终找到了解决方法。 The problem seems to be in .val( ).问题似乎出在 .val() 中。 For some reason, jQuery wants to automatically fill inputs from cache when it is used to get/set inputs.出于某种原因,jQuery 希望在用于获取/设置输入时自动从缓存中填充输入。 In my situation, I was doing something like this:在我的情况下,我正在做这样的事情:

HTML File: HTML文件:

<form ...>
    <input type="hidden" name="my_value" value="X" />
</form>

JS File: JS文件:

$( document ).ajaxSuccess( function ( event, jqxhr, settings, data ) {
    if ( settings.dataType == 'json' && data && data.my_value ) {
        $( 'input[name="my_value"]' ).val( data.my_value );
    }
} );

If I refreshed the page without clearing the cache, the inputs would be automatically overwritten with a cached value from an AJAX request on the initial page request even if they were already filled with the value in the server response.如果我在不清除缓存的情况下刷新页面,则输入将自动被初始页面请求上的 AJAX 请求中的缓存值覆盖,即使它们已经填充了服务器响应中的值。 To solve the problem, I changed .val( ... ) to .attr( 'value', ... ), so the line:为了解决这个问题,我将 .val( ... ) 改为 .attr( 'value', ... ),所以这行:

$( 'input[name="my_value"]' ).val( data.my_value );

became变成了

$( 'input[name="my_value"]' ).attr( 'value', data.my_value );

So I think what you would need to do here to fix your issue is to simply change:所以我认为你在这里需要做的来解决你的问题就是简单地改变:

var lDescr = $('#FundNote').val();  // FundNote is the id of a textbox
var DocUrl = $('#DocUrl').val();  // DocUrl is the id of another textbox

to

var lDescr = $('#FundNote').attr( 'value' );  // FundNote is the id of a textbox
var DocUrl = $('#DocUrl').attr( 'value' );  // DocUrl is the id of another textbox

I'm not sure if this is jQuery itself or a plugin for it causing this, but it is really strange.我不确定这是 jQuery 本身还是它的插件导致的,但这真的很奇怪。 It just seems that if you use .val( ) at any point to get/set a value from/to an input, you'll cause your inputs' values to be corrupted with a cached value if you refresh or visit the page again later.似乎如果您在任何时候使用 .val( ) 来获取/设置来自/到输入的值,如果您稍后刷新或再次访问页面,您将导致输入的值被缓存值损坏. This corruption doesn't seem to happen when .val( ) is called, but rather when the document is loaded.这种损坏似乎不会在调用 .val() 时发生,而是在加载文档时发生。

To summarize, a call to .val( ) for get/set on inputs causes the value returned/set to be cached during the initial page request.总而言之,调用 .val() 以获取/设置输入会导致在初始页面请求期间缓存返回/设置的值。 Future requests will have the inputs' values corrupted after document load with the value returned/set during the initial request until you clear the browser cache.未来的请求将在文档加载后使用初始请求期间返回/设置的值破坏输入的值,直到您清除浏览器缓存。

Sometimes the browser caches form input values so if you return to the page you need not re-type stuff.有时浏览器会缓存表单输入值,因此如果您返回该页面,则无需重新键入内容。 To disable it, set <form ... autocomplete="off"> .要禁用它,请设置<form ... autocomplete="off"> This will block the form re-loading what the user typed last time on page load.这将阻止表单重新加载用户上次在页面加载时键入的内容。

It sounds like this isn't your concern, but rather your concern is the text in the input isn't what's return from .val() .听起来这不是您关心的问题,而是您关心的问题是输入中的文本不是.val()返回的.val() If that's the case, show some code, and let's dig in.如果是这种情况,请显示一些代码,然后让我们深入研究。

Don't cache the value (.val()), cache the jQuery object:不要缓存值 (.val()),缓存 jQuery 对象:

function saveData() {
var lDescr = $('#FundNote');  // FundNote is the id of a textbox
var DocUrl = $('#DocUrl');  // DocUrl is the id of another textbox
    alert( lDescr.val() );
    alert( DocUrl.val() );
  //ajax call to a controller
}

I had the same issue, after some tries, I discovered an alternative that works for me...我遇到了同样的问题,经过一些尝试,我发现了一种对我有用的替代方法......

$("#id").valueOf().valueOf().valueOf().val();

Could look some "weird" solution, but it works!可能看起来有些“奇怪”的解决方案,但它有效!

我遇到了同样的问题,我用attr("value")切换了val() attr("value")来修复它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM