日期:2014-05-16  浏览次数:20494 次

又遇到个蹊跷的问题
我的document.all.personinfo.innerText=str语句为什么不执行,alert(str)能顺利执行。纳闷了……
<html>
<head>
<script language="JavaScript">
  function person()
  {
  var s=document.forms[0],name,sex,age,str;
name =s.username.value;
for( var i=0;i<s.usersex.length;i++)
{
if(s.usersex[i].checked)
sex = s.usersex[i].value;
}
age = s.userage.options[s.userage.selectedIndex].value;
str="您的个人信息为:"+name+"---"+sex+"---"+age;
alert(str);
  document.all.personinfo.innerText=str;
  }
  </script>
 </HEAD>
 <BODY>
 <p id=personinfo></p>
<form onSubmit="person()">
  <input type="text" name="username">
  <input type="radio" name="usersex" value="男" checked>男
  <input type="radio" name="usersex" value="女">女
  <select name="userage">
  <option value=18>18
<option value=20>20
<option value=22>22
<option value=24>24
  </select>
  <input type="submit" name="submit" value="提交">
 </form>
 </BODY>
</HTML>

------解决方案--------------------
<html> 
<head> 
<script language="JavaScript"> 
function person() 

var s=document.forms[0],name,sex,age,str; 
name =s.username.value; 
for( var i=0;i <s.usersex.length;i++) 

if(s.usersex[i].checked) 
sex = s.usersex[i].value; 

age = s.userage.options[s.userage.selectedIndex].value; 
str="您的个人信息为:"+name+"---"+sex+"---"+age; 
alert(str); 
document.all.personinfo.innerText=str; 
return false;

</script> 
</HEAD> 
<BODY> 
<p id=personinfo> </p> 
<form onSubmit="return person()"> 
<input type="text" name="username"> 
<input type="radio" name="usersex" value="男" checked>男 
<input type="radio" name="usersex" value="女">女 
<select name="userage"> 
<option value=18>18 
<option value=20>20 
<option value=22>22 
<option value=24>24 
</select> 
<input type="submit" name="submit" value="提交"> 
</form> 
</BODY> 
</HTML>
------解决方案--------------------
貌似innerText是只读属性。

改成 innerHTML 试试
------解决方案--------------------
改成 innerHTML 试试
------解决方案--------------------
onSubmit="return person()"
不要再犯这样低级的错误

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script language="JavaScript">
function person()
{
var s=document.forms[0],name,sex,age,str;
name =s.username.value;
for( var i=0;i <s.usersex.length;i++)
{
if(s.usersex[i].checked)
sex = s.usersex[i].value;
}
age = s.userage.options[s.userage.selectedIndex].value;