当我创建一个文本输入表单并在其中写入一些名称时,代码将我的输入视为一个数字,当我输出它时,我有一个错误(NaN),某处我做错了请帮助我...... THX
<h1>Please enter your name</h1>
<input id="nameid" type="text">
var nameUser = document.getElementById("nameid");
var name = nameUser.toString();
document.write('<h2>' +'Worker name:\t'+ +name+ '<\h2>');
这是我正在使用的整个代码。
<script>
function doStuff()
{
var nameAge = document.getElementById("ageInput");
var age = parseInt(nameAge.value);
var nameSalary = document.getElementById("salaryInput");
var salary = parseFloat(nameSalary.value);
var nameUser = document.getElementById("nameid");
var name = nameUser.toString();
document.write('<h2>' +'Age experience:\t'+ +age+ '<\h2>');
document.write('<h2>' +'Worker name:\t'+ +name+ '<\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="number">
<h1>Please enter your salary($)</h1>
<input id="salaryInput" type="number">
<h1>Please enter your name</h1>
<input id="nameid" type="text">
<input type="button" value="Submit" onClick="doStuff()">
</html>
答案 0 :(得分:1)
这是一个问题:
var nameUser = document.getElementById("nameid");
var name = nameUser.toString();
你需要获得价值。改为:
var nameUser = document.getElementById("nameid");
var name = nameUser.value.toString();
然后改变:
document.write('<h2>' +'Worker name:\t'+ +name+ '<\h2>');
为:
document.write('<h2>' +'Worker name:\t' +name+ '<\h2>');
应该是好的。有关完整代码,请参阅http://jsfiddle.net/h6zPv/