简体   繁体   English

当我添加.sqrt()时,为什么我的jQuery无法正常工作

[英]Why will my jQuery not work when I add the .sqrt()

I am trying to create a grid of divs and need to take the square root of the users input to find the height and width needed to make a square. 我正在尝试创建一个div网格,需要使用用户输入的平方根来查找制作一个正方形所需的高度和宽度。 This is the code I have doing it: 这是我做的代码:

$('#size').click(function(){
 $('.container').empty();
 var big = prompt('Enter the desired number of cubes');
 var final = $(big).sqrt();
 drawGrid(final);

Here is a link to my whole project on jsfiddle: https://jsfiddle.net/ztwsptys/ Any help would be appreciated. 这是我整个jsfiddle项目的链接: https ://jsfiddle.net/ztwsptys/任何帮助将不胜感激。

Just do Math.sqrt(big) . 只需执行Math.sqrt(big) Assuming that you are sure that big is a Number . 假设您确定Number big You don't need to go crazy with wrapping your big variable with jQuery. 您无需为用jQuery包装big变量而发疯。 Make sure that big is actually a number. 确保big实际上是一个数字。 If you read the user input I can bet that the value is read as a string. 如果您阅读了用户输入,我敢打赌,该值将作为字符串读取。 You would need to ParseFloat or ParseInt it depending what kind of value you are expecting from the user. 您可能需要ParseFloatParseInt ,具体取决于您希望用户获得什么样的价值。

Use Math.sqrt(big); 使用Math.sqrt(big); instead of $(big).sqrt(); 而不是$(big).sqrt();

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM