如何从提示框中获取数值? [英] How to get numeric value from a prompt box?

查看:93
本文介绍了如何从提示框中获取数值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在HTML和jQuery和JavaScript中进行一些简单的数学计算,所以我想从用户那里获得输入。

对于输入,我尝试了这样做:

  var x = prompt(Enter a Value,0); 
var y = prompt(输入值,0);

但是我无法执行任何类型的计算,因为这些值是字符串。

请,任何人都可以告诉我如何将它们转换为整数。

解决方案

parseInt() parseFloat()是JavaScript中的函数它可以帮助你分别将值转换为整数或浮点数。



语法:



  parseInt(string,radix); 
parseFloat(string);




  • 字符串:字符串表达式为解析为数字。

  • :(可选,但高度鼓励)要使用的数字系统的基数 - 介于2和36.


示例:



  var x = prompt(输入数值,0); 
var y = prompt(输入值,0);
var num1 = parseInt(x);
var num2 = parseInt(y);

在此之后,您可以执行您想要的任何计算。

I was trying to do some simple mathematical calculations in HTML and jQuery and JavaScript, so I wanted to get input from user.
For input I tried doing this :

 var x = prompt("Enter a Value","0");
 var y = prompt("Enter a Value", "0");

But I am not able to perform any kind of calculations as these values are strings.
Please, can any one show me how to convert them into integers.

解决方案

parseInt() or parseFloat() are functions in JavaScript which can help you convert the values into integers or floats respectively.

Syntax:

 parseInt(string, radix);
 parseFloat(string); 

  • string: the string expression to be parsed as a number.
  • radix: (optional, but highly encouraged) the base of the numeral system to be used - a number between 2 and 36.

Example:

 var x = prompt("Enter a Value", "0");
 var y = prompt("Enter a Value", "0");
 var num1 = parseInt(x);
 var num2 = parseInt(y);

After this you can perform which ever calculations you want on them.

这篇关于如何从提示框中获取数值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆