如何通过激活一个按钮来增加一个数字,以便每次按钮增加时都能访问某个功能? [英] How can I increase a number by activating a button to be able to access a function each time the button is increased?
问题描述
我尝试创建一个按钮,当它被点击时(Onclick)
I am trying to create a button, that whenever it is clicked (Onclick)
<button onClick="showUser(<?php echo $b?>)">Back</button>
它会更改一个值(数字),以便数字可以被
能够显示信息。示例我正在调用按钮的下一个和后退 - 如果点击下一个按钮,我希望它每次点击它时都会增加它的值,以便在数字增加时,会根据该数字访问不同的信息,例如,当我第一次点击按钮(这将是值1)信息将是可见的,然后当我再次点击它会增加到(2),我将能够看到其他信息,问题是,每当我尝试增加值时,值只增加一次。
it changes a value (number) so that the number can be retrieved by a function that will be able to display information. Example I am calling the buttons next and back- if the button next is clicked, I want it to keep increasing it's value everytime I click it so that when it's number increases, different information will be accessed depending from that number, example when I first click the button (it would be value 1) information will be visible then when I click again it will increase to (2) and I will be able to see other information, the problem is that whenever I try to increase the value, the value increases only once.
我这样做是为了让信息显示出来,每次用户点击这些按钮后,会依次显示数据库中的信息。
我正在使用javascipt,php和sql。
I am doing this so that I will have information displayed and everytime the user clicks these buttons next and back, information sequentially will be shown- taken from a database. I am using javascipt, php and sql.
我说的可能吗?
Is what I am saying possible?
推荐答案
您可以使用一个对象,在其中设置需要在点击时更新的两个变量:
var obj = {
nextClicked:0,
prevClicked:0
};
You could use an object, in which you set the two variables that you need to update on click: var obj = { nextClicked : 0, prevClicked : 0 };
function buttonClick(type) {
if(type == "prev") {
obj.nextClicked++;
}
if(type == "next") {
obj.prevClicked++
}
}
<button type="button" onclick="buttonClick('next')">Next</button>
<button type="button" onclick="buttonClick('prev')">Prev</button>
由于您使用的是ajax,所以变量不会重置,除非刷新页面
Since you are using ajax, the variables would not reset, unless you refresh the page
这篇关于如何通过激活一个按钮来增加一个数字,以便每次按钮增加时都能访问某个功能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!