简体   繁体   中英

Pass value of a function to a global variable

In html, I have

 <form id="form">
        <input type="radio" name="stack" value="north" onClick="input(value)">north<br>
        <input type="radio" name="stack" value="east" onClick="input(value)" >east<br>
        <input type="radio" name="stack" value="west" onClick="input(value)">west<br>
        <input type="radio" name="stack" value="south" onClick="input(value)">south
</form>

And the way I thought of fetching selected radio is like,

 var input=function(x)
    {
           console.log(x);
     }

I actually first coded like,

 var input="north";
    var dd=function(x)
    {
       if(input==null)
       {
          return direction.map(function (c) {

        return data.map(function (d) {

            //console.log(d[c]);
                 return {x: d.month, y: d[c]};
             })    
         })
         }

            else{
                 return data.map(function (d) {
                   return {x: d.month , y : d[input]};
               }}

    }
    var dataIntermediate=dd(input);
    console.log(JSON.stringify(dataIntermediate));

But now I actually need to take the value of input to this function onclick and I am confused how to proceed. Please help.

First, don't use inline HTML event handling attributes ( onclick , etc.) as they create "spaghetti code", create anonymous global functions that modify the this binding and don't follow the W3C DOM Event Standard

Here's all you need to get the value of a radio button and then pass that value somewhere:

 var radVal = null; // Once the DOM is ready... window.addEventListener("DOMContentLoaded", function(){ // Get all the radiobuttons var btns = document.querySelectorAll("[name=stack]"); // Loop through them for(var i =0; i < btns.length; ++i){ // Set up a click event handling callback function btns[i].addEventListener("click", function(evt){ // That grabs the value from the clicked button radVal = evt.target.value; // You can call another function from here, but if that other function // needs the value, you don't need to pass it because you just set it // into a variable (radVal) which has a higher scope than this function foo(); // Or, you can not call another function from here and just call the // other function when you need to, but you will need to make sure that // this happens AFTER one of the radio buttons were clicked, otherwise // radVal will still be null }); } function foo(){ // Since this function can be called at any time, we should check to make // sure that one of the radio buttons has first been clicked. if(radVal){ // radVal is not null, so a radio button was clicked console.log("foo says value is: " + radVal); } else { // radVal is still null so no button has been clicked yet console.log("foo says no button was clicked"); } } // This will show the else message because this is being called // before the radio buttons have been clicked foo(); }); 
  <form id="form"> <input type="radio" name="stack" value="north">north<br> <input type="radio" name="stack" value="east">east<br> <input type="radio" name="stack" value="west">west<br> <input type="radio" name="stack" value="south">south </form> 

change input(value) to input(this.value) and ready

 var global; var input = function(x) { global = x; console.log(x); }; // Checking the global variable in realTime setInterval(function(){ document.querySelector("#globalVariableValue").innerText = global; },10); 
 <form id="form"> <input type="radio" name="stack" value="north" onClick="input(this.value)">north<br> <input type="radio" name="stack" value="east" onClick="input(this.value)" >east<br> <input type="radio" name="stack" value="west" onClick="input(this.value)">west<br> <input type="radio" name="stack" value="south" onClick="input(this.value)">south </form> <br /> <span id="globalVariableValue"></span> 

you need to start a function, after that specify an "ID" for the input and that will make the selection of vaule and running the function accurate.

for example:

    function something(){

if(document.getElementById('north').checked) {
      //do something
 } else {
      // do something else
 }

the input looks like

<input type="radio" id="north" name="stack" value="north" onClick="functionName(this.value)">north<br>

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