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

时间:2016-12-10 13:46:33

标签: javascript

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

我想要获取所选收音机的方式就像,

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

我实际上是第一次编码,

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

但是现在我实际上需要将输入的值带到onclick这个函数,我很困惑如何继续。请帮忙。

3 个答案:

答案 0 :(得分:0)

首先,不要使用内联HTML事件处理属性(onclick等),因为它们会创建“意大利面条代码”,创建修改this绑定的匿名全局函数,不要跟随 W3C DOM Event Standard

以下是获取单选按钮的值然后将该值传递到某处所需的全部内容:

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>

答案 1 :(得分:0)

将输入(值)更改为输入(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>

答案 2 :(得分:0)

你需要启动一个功能,然后指定一个&#34; ID&#34;对于输入,这将使选择vaule和运行函数准确。

例如:

    function something(){

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

输入看起来像

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