使用JavaScript将全局变量调用为两个函数 [英] Call a global variable into two functions using JavaScript

查看:120
本文介绍了使用JavaScript将全局变量调用为两个函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个全局变量:

var x = document.getElementById("Node").value;

该值由ID为节点"的文本框确定.

Where the value is determined by a text box with the ID of "Node".

如果根据输入到文本和下拉列表中的值创建预设注释,则我有两个功能不相同.如何将上述全局变量调用到这些函数中,并将其添加到创建的语句中.两种功能看起来相同:

I have two functions that don't same thing if creating preset notes based on values entered into text and drop downs. How do I call the above global variable into those functions and have it added to the created statement. Both functions look the same:

这对一个功能有效,但在有两个功能时无效.我所拥有的是几个文本框和下拉菜单.这些文本框之一是称为Node的全局值.节点填写完毕后,我希望能够将该值同时放入两个函数中.这是我两个人都有的.

This works for one function but not when there is two. What I have is several text boxes and drop downs. One of those text boxes is a global value called Node. When node is filled out, I want to be able to pull that value into both functions. Here is what I have for both.

//Declare Node as a Global Variable
var x = document.getElementById("Node").value;

//Populates the Summary box
function mySummary() {
    var a = document.getElementById("Field1").value;
    var b = document.getElementById("Field2").value;
    var c = document.getElementById("Field3").value;
    var d = document.getElementById("Field4").value;
    document.getElementById("Summary").innerHTML = a + " - " + b + " - " + c + " - " + x + " -" + d;
}

//Populates the Notes box
function myNotes() {
    var a = document.getElementById("Field5").value;
    var b = document.getElementById("Field6").value;
    var c = document.getElementById("Field7").value;
    var d = document.getElementById("Field8").value;
    var e = document.getElementById("Field9").value;
    document.getElementById("Notes").innerHTML = x + " - " + a + " / " + b + " - " + c + "% Offline - " + d + " - " + e + " - " + f;
}

问题在于X的值没有同时输出到两个函数中.输出进入文本区域

The issue is that the value of X is not being pulled into both functions at the same time for the output. The output goes into a text area

如果我在每个函数中创建一个局部变量,并让用户在每个文本框中输入相同的信息两次,则效果很好,但我希望他们只输入一次信息并将其拉入每个文本区域

If I create a local variable in each function and make the user enter the same information twice per text box it works fine, but I want them to only enter the information once and have it pulled into each text area

推荐答案

尝试一下

(function() {
    var x = document.getElementById("Node").value;
    document.getElementById("Node").onchange = function() {
      myNode()
    };
    document.getElementById("mySelect1").onchange = function() {
      myNotes()
    };
    document.getElementById("mySelect2").onclick = function() {
      summary()
    };
    
     function myNode() {
      x = document.getElementById("Node").value;
    }

    function summary() {
      var a = document.getElementById("mySelect1").value;
      var b = document.getElementById("mySelect2").value;
      document.getElementById("5").value = a + " - " + b + " - " + x;
    }

    function myNotes() {

      var a = document.getElementById("mySelect1").value;
      var b = document.getElementById("mySelect2").value;
      document.getElementById("4").value = a + " + " + b + " + " + x;
    }


  }





)();

Notes : <input type="text" id="4" /><br> Summary : <input type="text" id="5" /><br>
<select id="Node">
  <option value="w">w
  <option value="x">x
  <option value="y">y
  <option value="z">z
</select>
<select id="mySelect2">
  <option value="a">a
  <option value="b">b
  <option value="c">c
  <option value="d">d
</select>
<select id="mySelect1">
  <option value="1">1
  <option value="2">2
  <option value="3">3
  <option value="4">4
</select>

这篇关于使用JavaScript将全局变量调用为两个函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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