简体   繁体   English

无法弄清楚为什么javascript不存储我的变量

[英]Can't figure out why javascript isn't storing my variable

I'm trying to figure out why my javascript function isn't storing my JS variable. 我试图弄清楚为什么我的javascript函数没有存储我的JS变量。

Here's the issue. 这是问题。 I trigger my storeMacAddress function with an onclick event, and the alert shows that the 'userMac' variable gets the form value properly. 我用onclick事件触发了storeMacAddress函数,并且警报显示'userMac'变量正确获取了表单值。 I need to be able to use this value later on, so I need the value stored in a variable that my other functions can user. 稍后我需要能够使用此值,因此我需要将其存储在其他函数可以使用的变量中的值。

However, when I call my testVariable function, the console says that the 'usermac' variable is not defined. 但是,当我调用testVariable函数时,控制台会说未定义'usermac'变量。

Here's the javascript: 这是JavaScript:

function storeMacAddress()
{
    var userMac = document.getElementById("MacAddress").value
    alert("testing: " + userMac);
    return userMac;
}

function testVariable(MacAddress)
{
    alert(MacAddress);
}

Here's the html 这是HTML

<!DOCTYPE html>
<html>
<head>
<title></title>
<script src='./index.js'>
</script>
</head>
<body>

<h1></h1>
<p></p>
<div>
    <form id="userRequest">
    Mac Address: <input type="text" id="MacAddress"><br>
    <input type="submit"
    onclick="storeMacAddress();"
    value="Save Mac Address">

    </form>
</div>


<button onclick="
testVariable(userMac);
">Test</button>



</body>
</html>

Since you are defining the variable inside the function, it is not visible outside the function or even to other functions, hence we can define it outside where it will be accessible to all the functions, you can just assign values inside the function or access it inside the function! 由于您是在函数内部定义变量,因此在函数外部甚至其他函数都不可见,因此我们可以在所有函数都可以访问的外部定义变量,您可以在函数内部分配值或对其进行访问里面的功能!

  • Note: I have changed type attribute of input to text, so that the variable assignment is visible in the snippet! 注意:我已将input type attribute更改为text,以便变量分配在代码段中可见!

 var userMac; function storeMacAddress() { userMac = document.getElementById("MacAddress").value if (userMac) alert("testing: " + userMac); return userMac; } function testVariable(MacAddress) { if (MacAddress) alert(MacAddress); } 
 <!DOCTYPE html> <html> <head> <title></title> <script src='./index.js'> </script> </head> <body> <h1></h1> <p></p> <div> <form id="userRequest"> Mac Address: <input type="text" id="MacAddress"><br> <input type="button" onclick="storeMacAddress();" value="Save Mac Address"> </form> </div> <button onclick=" testVariable(userMac); ">Test</button> </body> </html> 

  1. You are creating the variable within the scope of the function, and not within a closure. 您正在函数范围内而不是在闭包内创建变量。 Learn about closure. 了解有关关闭的信息。
  2. You are wiring the function inline, which means that your return is completely useless. 您正在内联函数,这意味着您的返回完全没有用。 Look up the concept of “unobtrusive JavaScript”. 查找“不干扰JavaScript”的概念。 Keep your JS and HTML separate, if you don't use a view-binding layer, like JSX, and even if you did, keep your services and your view logic separate. 如果不使用像JSX这样的视图绑定层,则将JS和HTML分开,即使这样做,也应将服务和视图逻辑分开。
  3. You are using a submit, inside of a form. 您正在表单内部使用提交。 Even if this did what you wanted, the page would immediately be reloaded, and you would lose everything. 即使这样做符合您的要求,该页面也会立即重新加载,您将丢失所有内容。 Look up form actions. 查找表单操作。
  4. To submit to the server, your form field should have a name . 要提交到服务器,您的表单字段应具有name

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

相关问题 无法弄清楚为什么我的Javascript没有使用Bulma框架更改CSS - Can't figure out why my css isn't being changed by my Javascript using Bulma framework 似乎无法弄清楚为什么我的JavaScript无法在IE和Chrome中运行 - Can't seem to figure out why my javascript isn't working in IE and Chrome 无法弄清楚为什么javascript无法在我的php文件中工作 - Can't figure out why javascript isn't working in my php file 无法弄清楚为什么我的JavaScript没有做任何事情 - Can't figure out why my javascript isn't doing anything 我不知道为什么我的JavaScript无法正常工作…我正在将值动态传递给标签 - I can't figure out why my javascript isn't working… I'm dynamically passing values to a tag 试图在 Javascript 中创建一个“trie”。 无法弄清楚为什么我的方法没有正确添加孩子 - Trying to create a 'trie' in Javascript. Can't figure out why my method isn't adding the children correctly 无法弄清楚为什么它不起作用。 javascript调用数据键 - Can't figure out why it isn't working. javascript calling data-key 无法弄清楚为什么JS插件无法正常工作 - Can't figure out why JS plugin isn't working 似乎无法弄清楚该脚本为何不起作用 - Can't seem to figure out why the script isn't working 无法弄清楚为什么它不起作用了 - Can't figure out why it isn't working, anymore
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM