在 jquery 中声明静态变量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11077960/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Declaring static variable in jquery
提问by user1458312
Kindly guide me regarding declaring static variable in the following program. I want to increment value of the static variable lists.counter with click of button. But its not working.
请指导我在以下程序中声明静态变量。我想通过单击按钮来增加静态变量 list.counter 的值。但它不起作用。
<%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true"
CodeBehind="Default.aspx.cs" Inherits="finalspecific2_list._Default" %>
<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">
<script src="Scripts/jquery-1.4.1-vsdoc.js" type="text/javascript"></script>
<script src="Scripts/jquery-1.4.1.js" type="text/javascript"></script>
<script src="Scripts/jquery-1.4.1.min.js" type="text/javascript"></script>
<script type="text/javascript">
$(document).ready(function lists() {
var list = $('#myList li:gt(0)');
list.hide();
lists.counter = 0;
var username = $("#<%= uname.ClientID %>").val();
var pwd = $("#<%= pwd.ClientID %>").val();
$("#<%= Login.ClientID %>").click(function () {
lists.counter++;
alert(lists.counter);
lists.counter++;
});
$("#<%= Button1.ClientID %>").click(function () {
{
alert(lists.counter);
}
});
});
</script>
</asp:Content>
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
<asp:Button ID="Login" runat="server" Text="Login" />
<asp:Button ID="Button1" runat="server" Text="Login" />
</ul>
</asp:Content>
lists.counter should increase when any of the button is clicked and should increament for the subsequent clicks. Kindly guide in this matter.
当单击任何按钮时,lists.counter 应该增加,并且应该为后续点击增加。请指导此事。
回答by Sammy S.
You can use a closure here:
您可以在此处使用闭包:
$(document).ready(function() {
// The actual counter is contained in the counter closure.
// You can create new independent counters by simply assigning
// the function to a new variable
function makeCounter() {
var count = 0;
return function() {
count++;
return count;
};
};
// This variable contains a counter instance
// The counter is shared among all calls regardless of the caller
var counter = makeCounter();
// The handler is bound to multiple buttons separated by commas
$("#button, #another_button, #yet_another_button").click(function () {
var i = counter();
console.log("The counter now is at " + i);
// Probably update your counter element
// $("#counter").text(i);
});
});?
You can find the code in thisfiddle.
你可以在这个小提琴中找到代码。
If you're interested, you can read more about closures here.
如果您有兴趣,可以在此处阅读有关闭包的更多信息。
Update:This fiddleuses multiple buttons sharing the same counter.
更新:这个小提琴使用共享同一个计数器的多个按钮。
回答by Dipak
Replace lists.Counter
with this.Counter
and check if it works
替换lists.Counter
为this.Counter
并检查它是否有效
or try this -
或者试试这个 -
<input id="button" type="button" value="button" />
var counter = 0;
$("#button").click(function () {
console.log(counter++);
});
回答by Subhajit
While declaring variables in JQuery, please don't use DOT in between, instead you can use camel format to differentiate two words, so list.counter
should be listCounter
在 JQuery 中声明变量时,请不要在两者之间使用 DOT,而是可以使用骆驼格式来区分两个词,所以list.counter
应该listCounter
Changed code :
更改代码:
$(document).ready(function lists() {
listsCounter = 0;
$("#<%= Login.ClientID %>").click(function () {
listsCounter++;
});
});