![](/img/trans.png)
[英]global variable in java script pass value from inside to outside the function
[英]Pass value of a function to a global variable
在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函數,而我對如何繼續感到困惑。 請幫忙。
首先,在創建“意大利面條代碼”時不要使用內聯HTML事件處理屬性( onclick
等),創建會修改this
綁定且不遵循W3C DOM事件標准的匿名全局函數
這就是獲取單選按鈕的值,然后將該值傳遞到某處的全部操作:
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>
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>
您需要啟動一個函數,然后為輸入指定一個“ ID”,這將使有效值的選擇和准確運行該函數成為可能。
例如:
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>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.