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

SCRIPT5007: 无法设置属性“value”的值: 对象为 null 或未定义
function check()
  {
  var a=confirm("确定吗");
  if (a==true)
  {
  document.getElementById("lblsur").value="1";
   
  }
  else(a==false)
  {
  document.getElementById("lblsur").value="2";
  }
  }

代码执行时就报那个错了。请问改如何解决

------解决方案--------------------
首先你的判断错了, 其次label用javascript获取的话请使用document.getElementById("lblsur").innerHTML
------解决方案--------------------
我试了一下,这样是可以的。
HTML code

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm4.aspx.cs" Inherits="WebApplication1.WebForm4" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>无标题页</title>

    <script type="text/javascript">
    function check()
    {
      var a=confirm("确定吗");
      if (a)
      {
        document.getElementById("Label1").innerText="1";
      }
      else
      {
        document.getElementById("Label1").innerText="2";
      }
    }
     window.onload=check;
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:Label ID="Label1" runat="server"></asp:Label>
    </div>
    </form>
</body>
</html>