JavaScript运算符+dons'工作不正常
JavaScript operator + doesn't work normally
这个简单的代码需要一些帮助,如果我们输入"工作经验"的值,例如3,工资"1000",条件是工资应该在初始工资的基础上加10%,所以结果应该是"1100",但在我的公式中,它显示的结果是1000250,我观察到如果我把符号"+"改为"-",它正确地显示了"900",我做错了什么?
if (age>=3 && age<10) {
var increase_1;
var salary_2;
increase_1=(salary*10)/100;
salary_2=salary+increase_1;
document.write('<h4>' +'Proceeding from work experience the new salary was increase by 10%:'t'+ +salary_2+ '<'h4>');
此外,如果我使用的是像"salary=1000"这样的辩护值,该程序也没有问题。。等待一些答案,thx
<html>
<head>
</head>
<script>
function doStuff()
{
var nameAge = document.getElementById("ageInput");
var age = nameAge.value;
var nameSalary = document.getElementById("salaryInput");
var salary = nameSalary.value;
document.write('<h2>' +'Age experience:'t'+ +age+ '<'h2>');
document.write('<h2>' +'Starting salary ($):'t'+ +salary+ '<'h2>');
if (age>=3 && age<10) {
var increase_1;
var salary_2;
increase_1=(salary*10)/100;
salary_2=salary+increase_1;
document.write('<h4>' +'Proceeding from work experience the new salary was increase by 10%:'t'+ +salary_2+ '<'h4>');
}
else if (age>=10 && age<20){
var increase_2;
var salary_3;
increase_2=(salary*25)/100;
salary_3=salary+increase_2;
document.write('<h4>' +'Proceeding from work experience the new salary was increase by 25%:'t'+ +salary_3+ '<'h4>');
}
else if (age>=20){
document.write('<h4>' +'Proceeding from work experience you get a prize:'+ '<'h4>');
document.write('<img src="http://3.bp.blogspot.com/-2T_AGEs19_4/T_c2ERHsJeI/AAAAAAAIK9E/MGAQAa9ppDE/s800/2013-Mercedes-G-Class-AMG-011.jpg">');
}
else {
document.write('<h4>' +'Proceeding from work experience the salary is:'t' +salary+'<'h4>');
}
}
</script>
<h1>Please enter your work experience(years)</h1>
<input id="ageInput" type="text">
<h1>Please enter your salary($)</h1>
<input id="salaryInput" type="text">
<input type="button" value="Submit" onClick="doStuff()">
</html>
JavaScript看到一些从输入框中提取的值是文本字符串,而不是数值,并将它们简单地连接起来。
如果您正在从输入框中读取一个值,并希望在公式中使用它,则需要首先通过parseInt()运行它。例如
var age = parseInt(nameAge.value, 10);
或者,如果你想使用十进制值(浮点),你需要通过parseFloat()运行它
var salary = parseFloat(nameSalary.value);
将基数(10)作为第二个参数传递给parseInt()将阻止使用低于版本5的ECMAScript的旧浏览器将以0开头的数字解释为八进制值。
将输入解析为int(如果需要小数,则为float!),它读取为字符串,从而连接。
var salary = parseInt(nameSalary.value);
var age = parseInt(nameAge.value);
要从表单字段中获取数字,必须将其转换为数字,最快的方法是从该变量中减去零。
salary-=0;
increase_1=salary+((salary*10)/100);
salary_2=salary+increase_1;
你的工资"1000"将产生(int)1000,即*10=10000,然后/100=100,再加上最初的1000,即1100。
,你可以尝试使用一个函数或原型来为你做腿部工作
Number.prototype.percent = function(p){
return ((this-0)*p)/100;
}
那么你可以简单地。。。increase_1=salary+(salary.percent(10));
相关文章:
- Javascript.getHours()工作不正常
- removeClass函数没有'工作不正常
- jQuery Ajax GET请求工作不正常
- Cordova ng路线工作不正常
- 更新属性工作不正常Meteor/MongoDB
- 为什么文本对齐:对;工作不正常
- Javascript作用域和Ajax调用;工作不正常
- JavaScript DOM insertBefore'工作不正常
- Angular Js布线工作不正常
- onmouseout函数工作不正常
- 承诺工作不正常
- JQuery.live()工作不正常
- Alloy UI文本区域工作不正常
- ng src工作不正常,但src工作正常
- Canvas.toDataUrl(“image/png”)工作不正常
- JQuery `length`属性工作不正常.为什么?
- Javascript setInterval工作不正常
- 我的Javascript代码没有'工作不正常
- 表单验证工作不正常,在不检查条目的情况下继续
- $localStorage array select with ng选项工作不正常