Javascript没有';t在<;头部>;或者<;身体>;标签


Javascript doesn't work inside <head> or <body> tags

我试着把这个代码放进去:

<script>
    function myfunc()
    {
        var xmlhttp;
        if(window.XMLHttpRequest)
            xmlhttp = new XMLHttpRequest();
        else
            xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
        xmlhttp.onreadystatechange=function()
        {
            if(xmlhttp.readyState==4 && xmlhttp.status==200)
                alert(xmlhttp.responseText);
            else
                alert("Something went wrong!");
        }
        var tbl = document.GetElementByID("tbl_users");
        var str="";
        var tid;
        var isActive;
        for(x=0; x=tbl.rows[x]; x++)
        {
            tid=0;
            isActive="true";
            if(tbl.cells[1].checked)
                tid=1;
            else
                tid=2;
            if(tbl.cells[6].checked)
                isActive="true";
            else
                isActive="false";
            str="uid="+tbl.cells[0].value+"&tid="+tid+"&firstName="tbl.cells[2].value+"&lastName="+tbl.cells[3].value+"&userName="+tbl.cells[4].value+"&passWord="+tbl.cells[5].value+"&isActive="+isActive;
            xmlhttp.open("POST", "save.php", true);
            xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
            xmlhttp.send(str);
        }
    }
</script>

在我正在制作的网页的标签中,因为我只遵循了一些显示相同内容的教程。以下是我的HTML代码:用户

    <!--JS codes above here-->
</head>
<body>
    <form>
        <table align='center' border='1' name='tbl_users'>
            <tr>
                <td>UID</td>
                <td>Admin</td>
                <td>First Name</td>
                <td>Last Name</td>
                <td>Username</td>
                <td>Password</td>
                <td>Active</td>
            </tr>
            <?php
            while($row = mysql_fetch_array($table))
            {
                echo "
                    <tr>
                        <td><input type='text' name='uid' value='".$row['uid']."' disabled/></td>";
                        if($row['tid'] == 1)
                            echo "<td><input type='checkbox' name='tid' value='1' checked/></td>";
                        else
                            echo "<td><input type='checkbox' name='tid' value='2'/></td>";
                echo "  <td><input type='text' name='firstName' value='".$row['firstName']."' /></td>
                        <td><input type='text' name='lastName' value='".$row['lastName']."' /></td>
                        <td><input type='text' name='userName' value='".$row['userName']."' /></td>
                        <td><input type='text' name='passWord' value='".$row['passWord']."'/></td>";
                        if($row['isActive'] == 0)
                            echo "<td><input type='checkbox' name='isActive' value='true' checked/></td>";
                        else
                            echo "<td><input type='checkbox' name='isActive' value='false'/></td>";
                echo "</tr>";
            }?>
        <tr>
            <td colspan='7' align='right'><input type='button' value="Save" onclick='myfunc()'/></td>
        </tr>
        </table>
    </form>
</body>

现在,我已经阅读了可能的解决方案,如:-将其放入$(document).ready()调用中-将JS代码放入标签中,刚好位于结束标签的下方

但是,它仍然不起作用。

我尝试过简单的JS代码,比如:

<script> function myfunc(){alert("hello world!");} </script>

使用相同的解决方案,只有第二种方法对此有效。

知道吗?

我看到至少两个问题:

  1. JavaScript区分大小写,var tbl = document.GetElementByID("tbl_users");应该是var tbl = document.getElementById("tbl_users");(注意getElementById中的gd)。

  2. 在行中:

    str="uid="+tbl.cells[0].value+"&tid="+tid+"&firstName="tbl.cells[2].value+"&lastName="+tbl.cells[3].value+"&userName="+tbl.cells[4].value+"&passWord="+tbl.cells[5].value+"&isActive="+isActive;
    

    您在"&firstName="之后和tbl.cells[2].value之前缺少一个+。您的控制台应该告诉您您有一个Unexpected identifier

for(x=0; x=tbl.rows[x]; x++)应该是for(x=0; x<tbl.rows[x]; x++)

您需要在第二部分中使用比较运算符。

这是一个清理版本。请记住,您应该始终在if行的末尾使用大括号,因为JS编译器喜欢插入;否则

function myfunc() {
    var xmlhttp;
    if (window.XMLHttpRequest) {
        xmlhttp = new XMLHttpRequest();
    } else {
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    }
    xmlhttp.onreadystatechange = function() {
        if (xmlhttp.readyState === 4 && xmlhttp.status === 200) {
            alert(xmlhttp.responseText);
        } else {
            alert("Something went wrong!");
        }
    }
    var tbl = document.getElementById("tbl_users");
    var str = "";
    var tid;
    var isActive;
    for (x = 0; x < tbl.rows[x]; x++) {
        tid = 0;
        isActive = "true";
        if (tbl.cells[1].checked) {
            tid = 1;
        } else {
            tid = 2;
        }
        if (tbl.cells[6].checked) {
            isActive = "true";
        } else {
            isActive = "false";
        }
        str = "uid=" + tbl.cells[0].value + "&tid=" + tid + "&firstName=" 
                + tbl.cells[2].value + "&lastName=" + tbl.cells[3].value + "&userName="
                + tbl.cells[4].value + "&passWord=" + tbl.cells[5].value
                + "&isActive=" + isActive;
        xmlhttp.open("POST", "save.php", true);
        xmlhttp.setRequestHeader("Content-type",
                "application/x-www-form-urlencoded");
        xmlhttp.send(str);
    }
}