将值放在函数之外的全局变量中 [英] Put a value outside a Function in a global var

查看:88
本文介绍了将值放在函数之外的全局变量中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对不起,整个代码就是这个.我给你的简短形式要轻,但我犯了很多错误.因此,请忽略之前编写的代码,然后阅读.

I'm sorry guys, the entire code is this. I given you the short form to be light, but I made many mistakes. So please, ignore the code written before and read this.

在代码底部,您还可以找到对函数的调用(它们在另外两个file.html中)

At the bottom of the code you can find also the calls to the functions (They are in two other files.html)

问题在于,三个"window.alert"显示的值(在第二个Function运行期间)未定义,因为顶部没有三个声明的var或它们不是全局变量(var numero_giocatori ; var numero_lupi; var numero_villici;)

The problem is that the value that the three "window.alert" show (during the running of the second Function) is undefined, as there aren't the three declared var on top or they aren't global (var numero_giocatori; var numero_lupi; var numero_villici;)

(对不起,我多次更改了编辑.请与一个新手同好;-))

(I'm sorry, I changed the edit many times. Please be nice with a newbie guy ;-) )

enter code here
    //--------------------------------------------------------------------------------------------------------------//
//              IMPOSTAZIONI: NUMERO DI GIOCATORI                       //
//--------------------------------------------------------------------------------------------------------------//

var numero_giocatori;
var numero_lupi;
var numero_villici;

function validatore_numero_giocatori(numero_giocatori) {

var scritta_output;

    numero_giocatori = document.getElementById("digitazione_numero_giocatori").value;

        if (numero_giocatori < 8 || numero_giocatori > 24) {
            scritta_output = "Hai inserito un numero minore di 8 o maggiore di 24";
        } 
        else if (isNaN(numero_giocatori)) {
            scritta_output = "Non hai inserito un numero. <br> Inserire un numero da 8 a 24";
        }
        else {
            scritta_output = "OK!";
            alert(numero_giocatori);
            window.open("/home/enrico/Scrivania/cod-x/lup-x/gioco/impostazioni: numero lupi vs numero villici.html","_self");
        }
        document.getElementById("validazione").innerHTML = scritta_output;
    return numero_giocatori;
}
//--------------------------------------------------------------------------------------------------------------//
//              IMPOSTAZIONI: NUMERO DI LUPI VS VILLICI                                                         //
//--------------------------------------------------------------------------------------------------------------//

function scelta_modalità_di_selezione (numero_giocatori) {
    if (document.getElementById("proporzioni_random").checked){             //proporzionale random
        numero_lupi = Math.floor(Math.round(Math.random()*1) + numero_giocatori/4); //osservare bene il codice!!!
        numero_villici = numero_giocatori - numero_lupi;
        alert(numero_giocatori);
        alert(numero_lupi);
        alert(numero_villici);
        }
    else{
        alert("Ciao, ti ricordi di me?");
    }
    window.open("/home/enrico/Scrivania/cod-x/lup-x/gioco/impostazioni: creazione personaggi.html","_self");
}
//FIRST CALL HTML
<div id = "form1">
    Numero Giocatori:<input type="text" name="numero" id="digitazione_numero_giocatori"><br><br>
    <div>   
    Inserire un numero di giocatori da 8 a 24   
    <p id="validazione"></p>
    </div>
    <button type="button" onclick="validatore_numero_giocatori(numero_giocatori)">Avanti</button>
</div>

//SECOND CALL HTML
 <div id = "form2">
        Seleziona la modalità di gioco<br>
        <div>
        Random. lupi circa 1/4:<input type="checkbox" id="proporzioni_random"><br>
        Digita il numero dei lupi:<input type="text" id="proporzioni_a_scelta">
        </div>
        <button type="button" onclick="scelta_modalità_di_selezione()">Avanti</button>
</div>

推荐答案

用以下代码替换您的函数: 您需要访问输入元素的值.

Substitute your function with this: You need to access the value of the input element.

function validatore_numero_giocatori() {
  numero_giocatori = document.getElementById('digitazione_numero_giocatori').value;
  alert(numero_giocatori);
  window.open("/home/enrico/Scrivania/cod-x/lup-x/gioco/impostazioni: numero lupi vs numero villici.html","_self");
  document.getElementById("validazione").innerHTML = scritta_output;
  return numero_giocatori;
}

这篇关于将值放在函数之外的全局变量中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆