通过 html 按钮更改变量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15455289/
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
Changing variable by html button
提问by Kyrbi
I'm learning javascript and I decided to create simple Rock, Paper, Scissors game. I want to make it controllable by buttons. So I made this in html:
我正在学习 javascript,我决定创建简单的石头剪刀布游戏。我想让它可以通过按钮控制。所以我在 html 中做了这个:
<div id="game">
<button onClick="user(rock)">Rock</button>
<button onClick="user(paper)">Paper</button>
<button onClick="user(scissors)">Scissors</button>
<div id="result"></div>
<br>
<br>
<button onClick="test()">DEBUG</button>
</div>
and this in .js file.
这在 .js 文件中。
var user = "none";
function user(choice){
var user = choice;
}
function test(click){
alert("You chose " + user);
}
So I thought that after I click Rock button it will change var user to rock but it doesn't. After I click rock and then Debug button I get "You chose none".
所以我认为在我点击 Rock 按钮后,它会将 var user 更改为 Rock 但事实并非如此。在我点击摇滚然后调试按钮后,我得到“你没有选择”。
采纳答案by llvk
<div id="game">
<button onClick="choose('rock')">Rock</button>
<button onClick="choose('paper')">Paper</button>
<button onClick="choose('scissors')">Scissors</button>
<div id="result"></div>
<br>
<br>
<button onClick="test()">DEBUG</button>
</div>
and
和
var user;
function choose(choice){
user = choice;
}
function test(click){
alert("You chose " + user);
}
回答by Can Geli?
var
is used for declaring a variable. You don't need to declare user
variable again in user
function. You just need to assign a value to declared one.
var
用于声明变量。您不需要user
在user
函数中再次声明变量。您只需要为声明的值分配一个值。
var user; //declaration
function user(choice) {
user = choice; //assignment
}
回答by Steve Wellens
One problem:
一个问题:
var user = "none";
function user(choice){
var user = choice;
}
One variable of user is hiding the other variable of user.
用户的一个变量隐藏了用户的另一个变量。
And having a function and variable with the same name is a BAD idea.
拥有同名的函数和变量是一个坏主意。
回答by Johannes Pille
回答by Lucky Soni
Maybe try this.. cleaner markup.. uses jQuery
也许试试这个..更干净的标记..使用jQuery
<div id="game">
<button class="user" data-name="rock">Rock</button>
<button class="user" data-name="paper">Paper</button>
<button class="user" data-name="scissors">Scissors</button>
<div id="result"></div>
<br>
<br>
<button id="test">DEBUG</button>
</div>
$(document).ready(function() {
var user = "none";
$(".user").click(function() {
user = $(this).attr("data-name");
});
$("#test").click(function() {
alert(user);
});
});
回答by suspectus
The selections should be encased in the quotes, javascript is looking for the variables named paper etc.
选择应包含在引号中,javascript 正在寻找名为 paper 等的变量。
<button onClick="user(rock)">Rock</button>
<button onClick="user(paper)">Paper</button>
<button onClick="user(scissors)">Scissors</button>
回答by Anthony
Seems like a scoping issue. Try removing the var inside the function.
似乎是一个范围界定问题。尝试删除函数内的 var。
回答by romainberger
The other answers are fixing some issues. There is also a problem with the way you're calling the functions as you're passing rock
as a variable, you need to use a string:
其他答案正在解决一些问题。在rock
作为变量传递时调用函数的方式也存在问题,您需要使用字符串:
<button onClick="user('rock')">Rock</button>
Unless you're declaring the variables somewhere but it's not shown in your code.
除非您在某处声明变量但它没有显示在您的代码中。