通过 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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 06:34:36  来源:igfitidea点击:

Changing variable by html button

javascripthtmlvar

提问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?

varis used for declaring a variable. You don't need to declare uservariable again in userfunction. You just need to assign a value to declared one.

var用于声明变量。您不需要useruser函数中再次声明变量。您只需要为声明的值分配一个值。

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

The varkeyword used in the scope of a function will declare a new local variable.

var函数作用域中使用的关键字将声明一个新的局部变量。

Hence, in the global scope, userretains the value "none".

因此,在全局范围内,user保留值"none"

回答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);
    });
});

http://jsfiddle.net/rQDbe/

http://jsfiddle.net/rQDbe/

回答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 rockas 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.

除非您在某处声明变量但它没有显示在您的代码中。