【问题标题】:Uncaught TypeError: Cannot read property 'style' of null - style.display not working未捕获的类型错误:无法读取 null 的属性“样式” - style.display 不起作用
【发布时间】:2017-08-12 04:18:01
【问题描述】:

我的要求是我有一个带有注册信息的 jsp 页面。它将提交控制器中的所有值并返回到相同的 "Signup.jsp" 。到此为止,它正在工作。现在的要求是,一旦它返回到同一页面,我在从控制器重定向时设置了一个参数。在 Jsp 中,参数被验证,如果参数来自匹配的控制器,那么一个 'td' 将是可见的,当我第一次将 Jsp 发送到控制器时应该不可见。

这里是代码,“signup.jsp”在 WebContent -> WEB-INF -> jsp(folder)

<html>
<head>
 <!-- So this script below actually checks if from controller "31012057" 
 value is coming or not. If matches then "id=phone" in table will be visible 
 which is hidden now -->

 <script type="text/javascript">
  var value=<%= request.getAttribute("parameter") %>
  if(value!=null && value==31012057)
  {
  alert("My Phone text box will be visible here.......");
  document.getElementById('phone').style.display = 'visible';
  document.getElementById('phoneTextBox').style.display = 'visible';
  }
 </script>
 </head>
 <body>
<form:form id="signup" method="post" action="signup" modelAttribute="signup" 
commandName="signup">

<table>
 <tr>
 <td>
        <form:label path="firstname">FirstName</form:label>
 </td>
 <td>
       <form:input path="firstname" name="firstname" 
            id="firstname"/>
 </td>
 </tr>

 <tr>
 <td id="phone" style=display:none>
         <form:label path="phone">Phone</form:label>
 </td>
 <td id="phoneTextBox" style=display:none>
         <form:input path="phone" name="phone" id="phone" />
 </td>
 </tr>
 <tr>
  <td>
  <form:button id="register" name="register">Register</form:button>
  </td>
  </tr>
 </table>
</form:form>
 </body>
 </html>

我看到了一些样式错误,但在任何网站上都找不到关于如何在表单成功提交并且控制器从 jsp 接收所有信息并再次返回“signup.jsp”后使电话号码可见的任何解决方案。

如果有人想执行,那么这里是控制器代码。

    @RequestMapping(value = "/signup", method = RequestMethod.POST)
     public String signupPost(@ModelAttribute("signup") SignupPojo 
     signup,Model model) {

     int parameter=31012057;
     model.addAttribute("firstname", signup.getFirstname());
     model.addAttribute("parameter", parameter);
     return "signup"; 

SignupPojo.java 是 getter 和 setter,

     public class SignupPojo {

     private String firstname;
     private int phone;
     //getter setter
     }

web.xml

    <servlet>
    <servlet-name>signup</servlet-name>
    <servlet-class> 
     org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>2</load-on-startup>
   </servlet>
    <servlet-mapping>
    <servlet-name>signup</servlet-name>
    <url-pattern>/signup</url-pattern>
    </servlet-mapping>

signup-servlet.xml 位于 WebContent -> WEB-INF

     <?xml version="1.0" encoding="UTF-8"?>
  <beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="
    http://www.springframework.org/schema/beans
     http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
     http://www.springframework.org/schema/context
     http://www.springframework.org/schema/context/spring-context-3.0.xsd">

 <context:component-scan base-package="GIVE YOUR CONTROLLER PACKAGE NAME "/>

    <bean 
  class="org.springframework.web.servlet.view.InternalResourceViewResolver">
  <property name="prefix" value="/WEB-INF/jsp/" />
  <property name="suffix" value=".jsp" />
   </bean>
    </beans>

这将是一个很大的帮助。在此先感谢。

================================================ ================================ 好的,这是编辑部分。在@Ofisora 和@Tahir Hussain Mir 的帮助下,我解决了这个问题。解决方案是,由于加载问题,我已将“脚本”标签放在正文部分的末尾,就在关闭“正文”之前。而我变了 document.getElementById('phone').style.display = 'visible'; 至 document.getElementById('phone').style.display = 'block';

所以这是 "Uncaught TypeError: Cannot read property 'style' of null" 的解决方案,所以更改标题,适用于像我一样面临同样问题的任何人。

【问题讨论】:

  • 这是因为您的代码有style=display:none 而不是style="display:none"
  • @Ofisora 。不,style="display:none" 不能解决问题。
  • 你能把document.getElementById('phone').style.display = 'visible'; document.getElementById('phoneTextBox').style.display = 'visible';改成...display='block'吗?
  • @Ofisora 我没有得到你的最后一个问题。 style=display:none 是一个错字。它最初在我的代码中是 style="display:none" 。但这不是解决方案。此 style="display:none" 用于隐藏 'td'。一旦表单提交给控制器并再次返回此页面,我想使其可见。还有 document.getElementById('phone').style.display = 'visible';无法使此“td”可见。
  • visible 不是正确的值。应该是block

标签: javascript java css spring jsp


【解决方案1】:

解决问题的非常简单的方法就是将脚本代码放在 jsp 文件的末尾。 这就对了。 我首先想到,request.getAttribute 的转换是问题所在。 但是在查看了您的 cmets 之后,我明白了,脚本不知道您的 DOM这就是为什么错误是“无法读取 null 的样式 id”
原因是,脚本应该总是写在最后,这样他们就可以知道 DOM。否则,您也可以在开头使用 window.onload,以便您的脚本查看完整的 DOM。

所以只需将整个脚本标签放在文件末尾即可。

【讨论】:

  • 非常感谢。它奏效了。你救了我的命。但是想知道如何改变位置?
  • @SharmisthaSakar 我很高兴它对你有用。但是,你为什么不接受我的回答?
  • 我现在做到了。再次感谢。
  • 我很高兴亲爱的。 ^_^ @SharmisthaSakar
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-02-12
  • 2014-10-03
  • 1970-01-01
  • 1970-01-01
  • 2012-08-01
  • 2015-01-29
相关资源
最近更新 更多