添加1 Substract 1 onclick

时间:2015-12-01 19:20:58

标签: javascript add

我正在尝试创建一个表单。

目标是:

  1. 总共28名玩家(ID =总玩家)
  2. 分享了4组7名球员。 (group = 4 AND position = 7)
  3. 发送表格。
  4. 当点击右箭头时,为ID和位置添加+1。
  5. 当position = 7时,将+1添加到组。
  6. 我的问题是,当我到达第7位时,小组不会添加+1。

    谢谢,

    0, slice(None, None, None)
    	/*
    	function changeImageUp() will
    	
    	Add +1 to ID IdNum until hit 28 come back to 1.
    	Add +1 to ID Postcounter hit 7 come back to 1.
    	Add +1 to ID Groupcounter each time ID Postcounter hit 7.
    	When ID Groupcounter hit 4 come back to 1.
    	*/
    	function changeImageUp()
    	{	
    
    			positionID = document.getElementById("IdNum").getAttribute("name");
    			
    			
    			if(positionID == 28)
    			{
    				positionID = 0;
    			}
    			
    
    			var newPosition = parseInt(positionID) + 1;
    			
    
    			document.getElementById("IdNum").setAttribute("name", newPosition);
    			
    
    			document.getElementById("IdNum").innerHTML = "<input type= 'checkbox' name='ID' value='" + newPosition + "'checked> " + newPosition;
    			
    			
    
    			positionSpot = document.getElementById("Poscounter").getAttribute("name");
    			
    
    			if(positionSpot == 7)
    			{
    				positionSpot = 0;
    			}
    			
    
    			var newPositionSpot = parseInt(positionSpot) + 1;
    			
    
    			document.getElementById("Poscounter").setAttribute("name", newPositionSpot);
    			
    
    			document.getElementById("Poscounter").innerHTML = "<input type= 'checkbox' name='Position' value='" + newPositionSpot + "'checked> " + newPositionSpot;
    			
    			
    
    			GroupSpot = document.getElementById("Groupcounter").getAttribute("name");
    			if(GroupSpot == 4)
    			{
    				GroupSpot = 0;
    			}
    			
    			
    			if(positionID < 7)
    			{
    				GroupSpot = 1;
    				document.getElementById("Groupcounter").innerHTML = "<input type= 'checkbox' name='Position' value='" + GroupSpot + "'checked> " + GroupSpot;
    			}
    			else if(positionID > 6)
    			{
    				GroupSpot = 2;
    				document.getElementById("Groupcounter").innerHTML = "<input type= 'checkbox' name='Position' value='" + GroupSpot + "'checked> " + GroupSpot;
    			}
    			else
    			{
    				GroupSpot = 3;
    				document.getElementById("Groupcounter").innerHTML = "<input type= 'checkbox' name='Position' value='" + GroupSpot + "'checked> " + GroupSpot;
    			}
    
    			
    	}
    	
    	
    
    
    		function changeImageDown()
    	{	
    			positionID = document.getElementById("IdNum").getAttribute("name");
    			if(positionID <= 1)
    			{
    				positionID = 29;
    			}
    			var newPositionID = parseInt(positionID) - 1;
    			
    			document.getElementById("IdNum").setAttribute("name", newPositionID);
    			document.getElementById("IdNum").innerHTML = newPositionID;
    			
    			positionSpot = document.getElementById("Poscounter").getAttribute("name");
    		
    			if(positionSpot <= 1)
    			{
    				positionSpot = 8;
    				if(GroupSpot == 1)
    				{
    				GroupSpot = 5;
    				}
    				var newGroupSpot = parseInt(GroupSpot) - 1;
    				document.getElementById("Groupcounter").setAttribute("name", newGroupSpot);
    				document.getElementById("Groupcounter").innerHTML = newGroupSpot;
    			}
    			var newPositionSpot = parseInt(positionSpot) - 1;
    			document.getElementById("Poscounter").setAttribute("name", newPositionSpot);
    			document.getElementById("Poscounter").innerHTML = newPositionSpot;
    			
    			
    			GroupSpot = document.getElementById("Groupcounter").getAttribute("name");
    	}

1 个答案:

答案 0 :(得分:0)

如果你想滚动数字(如果达到极限则设置为零)你应该使用模运算%代替(实际上只是JavaScript中除法的其余部分),所以

positionID = positionID % 28;

positionSpot = positionSpot % 7;

GroupSpot = GroupSpot % 4;

根据您的表格

ID: 1-7 Group = 1
ID: 8-14 Group = 2
ID: 15-21 Group = 3
ID: 21-28 Group = 4

从代码开始为零

ID: 0-6 Group = 0
ID: 7-13 Group = 1
ID: 14-19 Group = 2
ID: 20-27 Group = 3

Group的数量是将ID除以7的结果,因此Group的值可以直接由ID的值计算{ {1}}(如果基于零)。

顺便说一句:如果你添加一个或减去一个,只要符号保持不变就不应该有所不同,所以你应该能够在一个函数中完成所有这些。

编辑添加了一些示例代码

GroupSpot = Math.floor(positionID / 7)

如果它向左翻滚全零,我不知道你想做什么,所以根据你的需要调整。