我有以下代码,但无法从脚本中访问“myVariable”——我假设是因为它是一个函数,而“myVariable”需要是一个全局变量。如何创建一个全局变量?
(注意:这是 Visual Studio 中 ASP.NET MVC 项目中的 .cshtml 文件)
<!DOCTYPE html>
<html>
<head>
</head>
<body>
@{var myVariable = "this is what I want to access and change later in the script";}
<div class="navbar navbar-inverse navbar-fixed-top">
<div class="container">
<div class="navbar-header">
...
</div>
</div>
</div>
<script>
$('[name="optradio1"]').on('change', function () {
$('#accordion-second').slideToggle();
$('#accordion-first').slideToggle();
alert(myVariable);
})
</script>
</body>
</html>