2016-12-10 5 views
0

In html, ich habePass Wert einer Funktion auf eine globale Variable

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

Und die Art, wie ich gedacht Holen der ausgewählten Radio wie ist,

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

ich zum ersten Mal wie eigentlich codiert,

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

Aber jetzt muss ich tatsächlich den Wert der Eingabe für diese Funktion onclick nehmen und ich bin verwirrt, wie es weitergeht. Bitte helfen Sie.

+0

Änderung Eingang (Wert) an den Eingang (this.value) und bereit –

Antwort

0

Änderung Eingang (Wert) an den Eingang (this.value) und bereit

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>

+0

Ist es möglich, console.log (global) als den entsprechenden Wert außerhalb der Funktion zu bekommen? – Gayathri

+0

Weil beim Laden global vor dem Ausführen einer Aktion Null zugewiesen würde – Gayathri

+0

Sicher. Führen Sie einfach die Funktionen in der richtigen Reihenfolge aus. Nach der Aktion ändert sich der globale Wert. –

0

Zuerst verwenden Sie keine Inline-HTML-Ereignisattribute Handling (onclick, etc.), wie sie "Spaghetti-Code", erstellen Sie anonyme globale Funktionen erstellen, ändern die this Bindung und folgen nicht dem W3C DOM Event Standard

Hier ist alles, was Sie den Wert eines Optionsfeld erhalten müssen und dann irgendwo diesen Wert übergeben:

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>

+0

sieht habe ich versucht, dies aber RadVal blieb null außerhalb der Funktion – Gayathri

+0

Wenn Sie den "var radVal" außerhalb von "input" behalten und Sie Ihre zweite Funktion nicht aufrufen, bevor Sie einen Radiobutton ausgewählt haben, würde das nicht passieren. –

+0

@Lalitha Siehe meine aktualisierte Antwort –

0

benötigen Sie eine Funktion zu starten, danach eine „ID“ für die Eingabe angeben, und das wird machen die Auswahl von vaule und das Ausführen der Funktion genau.

zum Beispiel:

function something(){ 

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

der Eingang wie

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

Ich möchte die Methode ab sofort nicht stören. – Gayathri

Verwandte Themen