使用时无法在JavaScript中使用HTML变量在浏览器中打印整个JavaScript代码

时间:2018-06-10 04:46:30

标签: javascript html

这是我无法从html表单访问JavaScript变量的注册表单,而是在浏览器中打印整个JavaScript代码。我没有在这里包含CSS文件,因为它是正确的请使用这些代码。

HTML代码

<form action = "book.js" name = "myform" onsubmit="return hello();">
<label for = "f_name" class = "ft"> First Name: </label>
<input  type = "textbox" name = "f_name" class = "desi" placeholder = "f_name">
<br><br>
<label for = "l_name" class = "ft"> Last Name: </label>
<input type = "textbox" name = "l_name" class = "desi" placeholder = "l_name">
<br><br>
<label for = "er_no" class = "ft"> Enrollment Number: </label>
<input type = "textbox" name = "er_no" class = "desi" placeholder = "10characters">
<br><br>
<label for = "sem" class = "ft"> Semester: </label>
<input type = "textbox" name = "sem" class = "desi"  placeholder = "less than eight">
<br><br>
<label for = "c_p"class = "ft"> Create Password: </label>
<input type = "password" name = "c_p" class = "desi" placeholder = "8 alphanumeric">
<br><br>
<label for = "e_p" class = "ft"> Enter Password: </label>
<input type = "password" name = "e_p" class = "desi" placeholder = "8 alphanumeric">
<br><br>
<input type = "submit" style = "background-color:yellow;color:red">
</form>

JavaScript代码

当我在浏览器中运行时,将显示整个JavaScript代码 下方。

    function hello()
    {

        var f_names = document.myform.f_name.value;
        var l_names = document.myform.l_name.value;
        var er_nos = document.myform.er_no.value;
        var sems = document.myform.sem.value;
        var c_ps = document.myform.c_p.value;
        var e_ps = document.myform.e_p.value;
        for(var i=0;i<er_no.length;i++)
        {
            if(!(er_no[i]>=0 && er[i]<=9))
            {
                alert("wrong enrollment number");
                return(false);
            }
        }
        if(sems>'8')
        {
            alert("invalid semester");
            return(false);
        }
    document.write("hello");
    return(true);
    }   

1 个答案:

答案 0 :(得分:0)

要执行而不是显示JavaScript代码,您需要将JavaScript代码封装在<script>标记中,如下所示。

<script type="text/javascript">
// your code
</script>