首页 > 解决方案 > 从一个函数中获取值并将其放入另一个函数

问题描述

我需要将函数中inputScore当前保存的值获取extractVariableassignValue函数中(此函数将值提供inputScore给 Index.html 页面)。我试图存储inputScore在一个全局对象中来解决范围问题,但里面assignValueglobal.scoreValue变量是未定义的。请问如何将 inputScore 输入到 assignValue 中?编程新手 - 任何帮助表示赞赏。谢谢。

global = {};


function extractVariable(inputScore) {

   global['scoreValue'] = inputScore;

};


function assignValue() { 

    document.getElementById("inputScore").value = global.scoreValue;

};

感谢大家的帮助。我非常接近解决我需要做的事情。问题似乎正在inputScore进入Index.html页面。我应该首先发布我的所有代码。道歉。index.html是一个单独的文件,其中包含指向 javascript 文件 ( Game.js) 的链接。我已经测试了链接并且它正在工作。当在 中按下按钮时Game.jsindex.html加载、读取 中的assignValue函数Game.js,并将玩家得分 ( inputScore) 插入到表单中的输入值中。目前插入表格的所有内容是: 我无法弄清楚为什么它不起作用。我已经包含了下面两个文件中的代码。再次感谢任何帮助。在此处输入图像描述

Game.js 代码:

function extractVariable(inputScore) {

return inputScore;


};


function assignValue(inputScore) {  
     document.getElementById("playerScore").value = inputScore;

};



var CrystalRunner = CrystalRunner || {};

CrystalRunner.GameState = {

init: function() {
  //...code here
  }, 


create: function() {
 //...code here
  },  


 update: function() {  
//..code here
//check if the player needs to die
      if(this.player.top >= this.game.world.height) {
         this.gameOver();
      }
  },  


 gameOver: function(){

    //..code here

    this.updateHighscore();

    //..code here

   },


  updateHighscore: function(){
    this.highScore = +localStorage.getItem('highScore');


    if(this.highScore < this.myScore){
            this.highScore = this.myScore;

            this.inputScore = this.highScore; 


            this.submitScoreButton = this.game.add.sprite(this.game.world.centerX-135, this.game.world.centerY+100, 'submitScoreButton');

            this.submitScoreButton.events.onInputUp.add(function() {

                    window.location.href = "index1.php"; 

              }, this);

        extractVariable(this.inputScore);
      }


      localStorage.setItem('highScore', this.highScore);
  },


};

索引.html 代码:

<?php
require_once 'dbconnect.php';

?>
<!DOCTYPE html>
<html lang="en">
<head>
    <title>Crystal Candy Game Login</title>

    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

    <link rel="stylesheet" href="css/bootstrap.min.css" type="text/css"/>
    <link href="css/style.css" rel="stylesheet">


</head>


<body onload="assignValue(extractVariable())" class="bg"> 


    <div id="preloader">
        <div id="status">&nbsp;</div>
    </div><!--preloader-->


    <div class="wrapper">


        <div id="main">

        <!-- Form -->
        <form id="form-style" method="post" action="crystalhandle.php" autocomplete="off">


                    <div class="form-group"> 
                        <label class="header-text"><span>First Name</span></label>
                        <input class="form-control" type="text" id="name" name="username" placeholder="Name" title="Please enter your Firstname" required="">
                    </div>


                    <div class="form-group"> 
                        <label class="header-text"><span>Score</span></label>
                        <input class="form-control" type="tel" id="playerScore" name="score" value="" readonly>
                    </div>


                    <div class="w3ls-btn form-group">       
                        <div class="wthreesubmitaits">
                        <input type="submit" name="signup" id="reg" class="button" id="next1" value="Send" style="font-family: sans-serif; font-size: 17px; font-weight: bold;"
                        </div> 
                    </div>


        </form>


        </div>

    </div>

<div id="bodytext"></div>


     <script type='text/javascript' src='js/jquery-2.2.3.min.js'></script>
     <script type="text/javascript" src="js/phaser.min.js"></script>
     <script type="text/javascript" src="js/states/Game.js"></script>

         <script>
            $(window).on('load', function() {
            $('#status').fadeOut(); 
            $('#preloader').delay(350).fadeOut('slow'); 
            $('body').delay(350).css({'overflow':'visible'});


            })
    </script>

</body>


</html>

我已经做了以下更改,我认为解决方案就在眼前。在Index.html我通过删除更改以下行inputScore

<body onload="assignValue(extractVariable())" class="bg"  >

Game.js我发现如果我将一个值硬编码到extractVariable函数中(见下文),硬编码的值将传递到我想要它执行的标记的value属性中。但是,我仍然无法弄清楚为什么这只适用于硬编码值?<input>Index.html

function extractVariable(inputScore) {
  inputScore = 27; //hard coded value works. Why?
return inputScore; 

};


function assignValue(inputScore) {  
  console.log(inputScore); //this console.logs the hard coded value from 
  //extractVariable like it should do
     document.getElementById("playerScore").value = inputScore;

};

标签: javascripthtmlglobal-variables

解决方案


您可以通过返回值来做到这一点,extractVariable然后将其作为参数传递给assignValue

function extractVariable(inputScore) {
  return inputScore;
};


function assignValue(inputScore) {
  document.getElementById("inputScore").value = inputScore;
};

assignValue(extractVariable(inputScore));

推荐阅读