简体   繁体   English

如何使我的var更改为与下拉菜单中的值相同的值?

[英]How can I make it so my var is changed to the same as the value in my dropdown menu?

I have some code which draws a parametrical circle and a var that dictates how many points it is made up of. 我有一些代码绘制一个参数化圆和一个变量,该变量决定了它由多少个点组成。 I can't figure out any way to change the var according to what option I pick in the dropdown menu. 我无法找到任何方法根据我在下拉菜单中选择的选项来更改var。

<!-- Dropdown -->
<select id="myList" onchange="PointsAmount()">
    <option>2</option>
    <option>4</option>
    <option>8</option>
    <option>16</option>
    <option>32</option>
    <option>64</option>
    <option>128</option>
</select>
var points = 16;
var step = 2*Math.PI/points;
var h = 150;
var k = 150;
var r = 50;

function PointsAmount(){
  var points = document.getElementById("myList").value;
}
//Circle draw
ctx.beginPath();

for(var theta=0;  theta < 2*Math.PI;  theta+=step){ 
    var x = h + r*Math.cos(theta);
    var y = k - r*Math.sin(theta);
    ctx.lineTo(x,y);
}

ctx.closePath();
ctx.stroke();

You are using var points = document.getElementById("myList").value; 您正在使用var points = document.getElementById("myList").value; . What you are doing is basically redefining points inside the function. 您要做的基本上是在函数内部重新定义points All you need to do is to remove the word var from the start of the line and it will work as intended. 您需要做的就是从行首删除var一词,它将按预期工作。

 var points = 16; var step = 2 * Math.PI / points; var h = 150; var k = 150; var r = 50; function PointsAmount() { points = document.getElementById("myList").value; console.log(points); } 
 <select id="myList" onchange="PointsAmount()"> <option>2</option> <option>4</option> <option>8</option> <option>16</option> <option>32</option> <option>64</option> <option>128</option> </select> 

You can double check what you did before with calling a function to print the value of points every time the value is changing: 您可以通过调用函数在每次值更改时打印点的值来仔细检查之前所做的事情:

 var points = 16; var step = 2 * Math.PI / points; var h = 150; var k = 150; var r = 50; function PointsAmount() { var points = document.getElementById("myList").value; console.log('inner var points=' + points); printPoints(); } function printPoints() { console.log('global var points=' + points); } 
 <!-- Dropdown --> <select id="myList" onchange="PointsAmount()"> <option>2</option> <option>4</option> <option>8</option> <option>16</option> <option>32</option> <option>64</option> <option>128</option> </select> 

You can see that the global definition of points is always 16, and that the inner function variable you defined there is changing, not effecting the global one. 您可以看到, points的全局定义始终为16,并且您在其中定义的内部函数变量正在更改,而不会影响全局变量。 That is the idea of scopes. 这就是范围的想法。 The var points exist only inside the scope of PointsAmount but not out of it. PointsAmount var points仅存在于PointsAmount范围内,但不存在于该范围PointsAmount

暂无
暂无

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

相关问题 如何让我的悬停下拉菜单在我的轮播上保持可见 - How can I make my hover dropdown menu stay visible over my carousel 如何修复代码以获取使下拉菜单正常工作的功能? - How can I fix my code to get the functions to make the dropdown menu work? 如何确保页面已加载且信息已更改,以便我的爬虫不会爬取相同的数据? - How do I make sure the page is loaded and the information has changed so my crawler wouldn't crawl the same data? 如何根据下拉菜单选择值预填表单? - How can I prefill my form based on a dropdown menu selection value? 如何限制下拉菜单上显示的选项数量? - How can I limit the number of options that show on my dropdown menu? 如何在更改值而不是单击按钮时运行函数? - How can I make my function run when a value is changed instead of when the button is clicked? 当我将鼠标悬停在文本上时,它会改变颜色并且颜色保持不变,我该如何做到这一点? - How do I make it so when I hover over my text it changes color and the color stays changed? 如何使现有菜单变成响应式下拉菜单? - How do I make turn my existing menu into a responsive dropdown menu? 如何隐藏下拉菜单? - How do I hide my dropdown menu? 当用户将鼠标悬停在 div 上但停止时关闭时,如何使我的下拉菜单保持打开状态? - How can I make my dropdown menu remain open when user is hovering over the div but close when they stop?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM