简体   繁体   English

jQuery Spinner加载值/刷新值

[英]Jquery Spinner Load Value/Refresh Value

I am using the jquery spinner within an MVC application. 我在MVC应用程序中使用jquery旋转器。 I implement its HTML and JS within the cshtml file. 我在cshtml文件中实现了它的HTML和JS。

<p>
    <label for="spinner">Select a value:</label>
    <input id="spinner" name="value">
</p>
<script>
     var spinner = $("#spinner").spinner();
     spinner.spinner("value", 10);
</script>

When the spinner loads and is initiated for the first time I want it to take the value of 10, however when the user has interacted with it and changed the value on subsequent refreshes of the page I want user selected number to be maintained, not it returned back to 10. 当微调器加载并首次启动时,我希望它取值为10,但是当用户与之交互并在随后的页面刷新时更改该值时,我希望保留用户选择的编号,而不是回到10。

As I am new to JS then the only way I can think of achiving this is reading/writing to a database which holds the number, 10 as default until it is over written? 因为我是JS新手,所以我能想到的唯一方法是读/写一个保存有数字的数据库,默认为10,直到被覆盖为止。

I thought I would ask in case there is a slicker solution? 我以为我会问,如果有一个更好的解决方案?

是的,如果要在刷新整个页面期间保持用户设置的值不变,则需要将其存储在某个位置,例如在localStorage中

There are some ways to do this. 有一些方法可以做到这一点。

Situation: The user interacts with the spinner and refresh the page (this is happening only in user PC, no submit button has been pressed). 情况:用户与微调框互动并刷新页面(这仅在用户PC上发生,没有按下提交按钮)。

Solutions: For this situation you can store your spinner value in Cookies. 解决方案:在这种情况下,您可以将微调器值存储在Cookies中。 (Cookie keep data on client side and they are made available on server side) (Cookie将数据保留在客户端,并且可以在服务器端使用它们)

Or you can make an async call to server that update the value in Session (Session keep data on server side and is unique for each user) or, as you said, in Database (Database keep data on server side and you have to make sure that the value is retrieved for the right user). 或者,您可以对服务器进行异步调用,以更新会话中的值(会话将数据保留在服务器端,并且对于每个用户而言都是唯一的),或者,正如您所说的,在数据库中 (数据库将数据保留在服务器端,您必须确保为正确的用户检索值)。

Note: I don't write the code for this because I want you to research about this (I know you can do it ). 注意:我不为此编写代码,因为我希望您对此进行研究(我知道您可以做到 )。

Knowing the difference between Cookies, Session and Database choose what fits best for you. 了解Cookie,会话和数据库之间的区别,然后选择最适合您的内容。

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

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