简体   繁体   English

将函数的值传递给全局变量

[英]Pass value of a function to a global variable

In html, I have 在HTML中,我有

 <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. 但是现在我实际上需要将输入值赋给onclick函数,而我对如何继续感到困惑。 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 首先,在创建“意大利面条代码”时不要使用内联HTML事件处理属性( onclick等),创建会修改this绑定且不遵循W3C DOM事件标准的匿名全局函数

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 将输入(值)更改为输入(this.value)并准备好

 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. 您需要启动一个函数,然后为输入指定一个“ ID”,这将使有效值的选择和准确运行该函数成为可能。

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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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