无法在validatename_degree中设置null的属性“innerHTML”

时间:2016-12-20 05:05:16

标签: javascript html

我已经在这里看到了这个错误的页面,但是div块中的window.load和put脚本都没有解决它。如果我写document.getElementById("fullname_error"),它正在工作,但它不能与document.getElementById(ident)一起工作,尽管var ident等于p的id,我已经检查了它。

<div class="most_page">
    <div id="form_container" >
        <form action="register.php" method="post" name="reg_doc">
            <table class="table">
                <tr>
                    <td>Full name</td>
                    <td><input type="text" name="fullname" value="Full name" ></td>
                    <td><p id="fullname_error" > hh</p></td>
                </tr>
                <tr>
                    <td>Address</td>
                    <td><input type="text" name="address" onkeypress='validatename_degree(fullname.value,fullname_error.id);' value="Address"></td>
                </tr>
            </table>
            <button class="button" id="submitreg" ><input id="submit" type="submit" value="submit"></button>
            <script type="text/javascript">
                function validatename_degree(x,ident) {
                    var iden='"'+ident+'"';
                    document.getElementById(iden).innerHTML=iden;
                }
            </script>
        </form>
    </div>
</div>

2 个答案:

答案 0 :(得分:1)

您不需要NSValue只使用var iden='"'+ident+'"';。请试试这个。

&#13;
&#13;
var iden=ident;
&#13;
&#13;
&#13;

答案 1 :(得分:0)

只需将参数ident传递给document.getElementById属性并使用您现有的代码,下面是一个工作片段

&#13;
&#13;
<div class="most_page">
    <div id="form_container" >
        <form action="register.php" method="post" name="reg_doc">
            <table class="table">
                <tr>
                    <td>Full name</td>
                    <td><input type="text" name="fullname" value="Full name" ></td>
                    <td><p id="fullname_error" > hh</p></td>
                </tr>
                <tr>
                    <td>Address</td>
                    <td><input type="text" name="address" onkeypress='validatename_degree(fullname.value,fullname_error.id);' value="Address"></td>
                </tr>
            </table>
            <button class="button" id="submitreg" ><input id="submit" type="submit" value="submit"></button>
            <script type="text/javascript">
                function validatename_degree(x,ident) {
                  console.log(""+ident)
                    var iden='"'+ident+'"';
                    document.getElementById(ident).innerHTML=iden;
                }
            </script>
        </form>
    </div>
</div>
&#13;
&#13;
&#13;