Javascript 为什么不';触发事件时是否更新变量?
我对编程很陌生,所以能得到一些帮助会很好 为什么当我按下按钮时变量没有更新 html:Javascript 为什么不';触发事件时是否更新变量?,javascript,jquery,loops,switch-statement,var,Javascript,Jquery,Loops,Switch Statement,Var,我对编程很陌生,所以能得到一些帮助会很好 为什么当我按下按钮时变量没有更新 html: 我可能更熟悉C++,这是我第一次尝试在JavaScript /jQuery中做一个DO循环。 < P>我想这就是你要做的游戏。享受吧 <html> <head> <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script> <
<>我可能更熟悉C++,这是我第一次尝试在JavaScript /jQuery中做一个DO循环。 < P>我想这就是你要做的游戏。享受吧
<html>
<head>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<script>
$(document).ready(function(){
var val = 0; //Initial value of the input box
var player = parseInt($('#pl').html()); //Player score
var pc = parseInt($('#pc').html()); //pc score
var total = parseInt($('#a').html()); //Total left
$('#turn').click(function () {
val = parseInt($('#val').val());
console.log(val);
console.log(total);
if(total > 0){
switch (val) {
case 1:
total -= 4;
player += 1;
pc += 3;
break;
case 2:
total -= 4;
player += 2;
pc += 2;
break;
case 3:
total -= 4;
player += 3;
pc += 1;
break;
}
$('#pl').html(player);
$('#pc').html(pc);
$('#a').html(total);
}
});
});
</script>
</head>
<body>
<h1>NIM</h1>
<p>Welcome to a simple edition of the game NIM</p>
<table>
<tr>
<th>You</th>
<th>Left</th>
<th>PC</th>
</tr>
<tr>
<td><p id="pl">0</p></td>
<td><p id="a">25</p></td>
<td><p id="pc">0</p></td>
</tr>
</table>
<br>
<p>How many do you want to pull?</p>
<input type="number" id="val" value="1" min="1" max="3">
<button id="turn">Trekk</button>
</body>
</html>
$(文档).ready(函数(){
var val=0;//输入框的初始值
var player=parseInt($('#pl').html();//玩家分数
var pc=parseInt($('#pc').html();//pc分数
var total=parseInt($('#a').html();//左总计
$(“#转动”)。单击(函数(){
val=parseInt($('#val').val());
控制台日志(val);
控制台日志(总计);
如果(总数>0){
开关(val){
案例1:
总数-=4;
玩家+=1;
pc+=3;
打破
案例2:
总数-=4;
玩家+=2;
pc+=2;
打破
案例3:
总数-=4;
玩家+=3;
pc+=1;
打破
}
$('#pl').html(播放器);
$('#pc').html(pc);
$('a').html(总计);
}
});
});
尼姆
欢迎来到一个简单版的游戏尼姆
你
左边
个人计算机
0
25
0
你想拉多少
跋涉
小提琴
你的错误
代码正在执行吗?我在这里创建了一个,变量正在更新。文本不会被更新,但这是因为在代码运行的任何地方都不会更新它。变量会发生变化。在
do
循环之后使用console.log()?
$('#turn').click(function () {
var val = parseInt($('#val').val()),
player = parseInt($('#pl').html()),
pc = parseInt($('#pc').html()),
total = parseInt($('#a').html());
console.log(val);
console.log(total);
do {
switch (val) {
case 1:
total -= 4;
player += 1;
pc += 3;
break;
case 'b2':
total -= 4;
player += 2;
pc += 2;
break;
case 'b3':
total -= 4;
player += 3;
pc += 1;
break;
}
} while (total > 1);});
<html>
<head>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<script>
$(document).ready(function(){
var val = 0; //Initial value of the input box
var player = parseInt($('#pl').html()); //Player score
var pc = parseInt($('#pc').html()); //pc score
var total = parseInt($('#a').html()); //Total left
$('#turn').click(function () {
val = parseInt($('#val').val());
console.log(val);
console.log(total);
if(total > 0){
switch (val) {
case 1:
total -= 4;
player += 1;
pc += 3;
break;
case 2:
total -= 4;
player += 2;
pc += 2;
break;
case 3:
total -= 4;
player += 3;
pc += 1;
break;
}
$('#pl').html(player);
$('#pc').html(pc);
$('#a').html(total);
}
});
});
</script>
</head>
<body>
<h1>NIM</h1>
<p>Welcome to a simple edition of the game NIM</p>
<table>
<tr>
<th>You</th>
<th>Left</th>
<th>PC</th>
</tr>
<tr>
<td><p id="pl">0</p></td>
<td><p id="a">25</p></td>
<td><p id="pc">0</p></td>
</tr>
</table>
<br>
<p>How many do you want to pull?</p>
<input type="number" id="val" value="1" min="1" max="3">
<button id="turn">Trekk</button>
</body>
</html>