jquery,是否可以将信息存储在变量而不是输入字段中?

时间:2021-02-13 16:45:44

I want to store the current page number in a jquery variable, but I am not sure if it is possible, so I am storing it in an input field. Am I doing it the most efficient way?

我想将当前页码存储在jquery变量中,但我不确定它是否可行,所以我将它存储在输入字段中。我这样做是最有效的方式吗?

<input type='hidden' id='current_page' /> 

$('#current_page').val(0);  

if page is changed,

如果页面被更改,

function (newpage){
$('#current_page').val(page_num);
}

3 个解决方案

#1


1  

You can use jQuery.data() for this -as mentioned by Nikita.

您可以使用jQuery.data() - 就像Nikita所提到的那样。

But if you'd like to support the JavaScript-disabled clients as well, then I'd stick to a hidden input element. This way you can still control the pagination from the server side on. The hidden input value can then be used to inform the server side what page the client is currently sitting in.

但是,如果您也想支持禁用JavaScript的客户端,那么我会坚持使用隐藏的输入元素。这样,您仍然可以从服务器端控制分页。然后,隐藏的输入值可用于通知服务器端客户端当前所在的页面。

#2


1  

With JQuery you can store data in any DOM element, input or not

使用JQuery,您可以将数据存储在任何DOM元素中,输入与否

$('#current_page').data('page', 0); // save value
$('#current_page').data('page'); // return value

http://api.jquery.com/data/

As I said, you don't have to add special html element for that, any one existing will work.

正如我所说,你不必为此添加特殊的html元素,任何一个现有的都可以使用。

#3


0  

The other 2 answers are correct, and you can also store it in a javascript variable if you want. Just use a closure. Something like this (but I'm not running this code ... it's conceptual only).

其他2个答案是正确的,如果需要,您也可以将其存储在javascript变量中。只需使用一个封闭。像这样的东西(但我没有运行这个代码......它只是概念性的)。

$(document).ready(function() {
   var pageNumber = 0;

   // This func can access "pageNumber" even
   //  though the var was declared in outer
   //  scope.
   function goToNewPage(newPageNumber) {
      pageNumber = newPageNumber;
   }

   // This func can access "pageNumber" even
   //  though the var was declared in outer
   //  scope.
   function getCurrentPageNumber() {
      return pageNumber;
   }

}

#1


1  

You can use jQuery.data() for this -as mentioned by Nikita.

您可以使用jQuery.data() - 就像Nikita所提到的那样。

But if you'd like to support the JavaScript-disabled clients as well, then I'd stick to a hidden input element. This way you can still control the pagination from the server side on. The hidden input value can then be used to inform the server side what page the client is currently sitting in.

但是,如果您也想支持禁用JavaScript的客户端,那么我会坚持使用隐藏的输入元素。这样,您仍然可以从服务器端控制分页。然后,隐藏的输入值可用于通知服务器端客户端当前所在的页面。

#2


1  

With JQuery you can store data in any DOM element, input or not

使用JQuery,您可以将数据存储在任何DOM元素中,输入与否

$('#current_page').data('page', 0); // save value
$('#current_page').data('page'); // return value

http://api.jquery.com/data/

As I said, you don't have to add special html element for that, any one existing will work.

正如我所说,你不必为此添加特殊的html元素,任何一个现有的都可以使用。

#3


0  

The other 2 answers are correct, and you can also store it in a javascript variable if you want. Just use a closure. Something like this (but I'm not running this code ... it's conceptual only).

其他2个答案是正确的,如果需要,您也可以将其存储在javascript变量中。只需使用一个封闭。像这样的东西(但我没有运行这个代码......它只是概念性的)。

$(document).ready(function() {
   var pageNumber = 0;

   // This func can access "pageNumber" even
   //  though the var was declared in outer
   //  scope.
   function goToNewPage(newPageNumber) {
      pageNumber = newPageNumber;
   }

   // This func can access "pageNumber" even
   //  though the var was declared in outer
   //  scope.
   function getCurrentPageNumber() {
      return pageNumber;
   }

}