简体   繁体   中英

Call a global variable into two functions using JavaScript

I have a global variable:

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

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:

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;
}

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

您可以简单地使用:

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

Try this

 (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> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM