js函数未定义是怎么回事,麻烦各位看看呀、

**每次都是这个错误提示:
(index):105 Uncaught ReferenceError: AddNewUser is not defined

at HTMLButtonElement.onclick ((inde**x):105)**
{% load staticfiles %}
<!DOCTYPE html>
<html lang="en-us">
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<title>Log in | Django site admin</title>
<link href="{% static 'assets/css/base.css'%}" rel="stylesheet" />
<link href="{% static 'assets/css/login.css'%}" rel="stylesheet" />
<link href="{% static 'assets/css/table.css'%}" rel="stylesheet" />
<script src="{% static "assets/js/jquery-1.8.3.min.js" %}"></script>

<script type="text/javascript">
function AddNewUser()
{
    var new_username = prompt("请输入用户名:");
    if (new_username != null){
        list = {{ data|safe }};
        var totalData = '';
        for( var i = 0; i < list.length; i ++ )
        {
            if(new_username == list[i])
                alert("当前用户已经加入了名单了,请重试");
        }

        $.get("/login/admin_addwhitelist", {"username": new_username}, function(ret){
                if(ret == "success")
                {
                alert("成功添加白名单");
                location.reload([true]);   
                }
                else
                {
                alert("白名单添加失败");
                }
                }
             );

    }else{
        alert("你按了[取消]按钮");
    }
}

function CreateRow(seq, row_data)
{
    var tableData = "<tr>"
    var real_seq = seq + 1;
    tableData += "<td width = '20%'>" + real_seq + "</td>"
    tableData += "<td width = '40%'>" + row_data + "</td>"
    //add a button
    href = "/login/admin_deletewhitelist?username=" + row_data;
    tableData += "<td width = '40%'>" + " <a href = " + href + " >Delete this user</a> " + "</td>";
    return tableData;
}

function CreateTable()
{
    if data == ""
        return;
    var list = {{ data|safe }};
    var totalData = '';
    for( var i = 0; i < list.length; i ++ )
    {
        totalData += CreateRow(i, list[i]);
    }
    $("#tbody1").html(totalData);
}

window.onload = CreateTable;
</script>

<meta name="robots" content="NONE,NOARCHIVE">
</head>


<body class=" login" data-admin-utc-offset="0">

<!-- Container -->
<div id="container">


<!-- Header -->
<div id="header">
<div id="branding">

<h1 id="site-name"><a href="/login/admin_index/">TSMC administration</a></h1>

</div>


</div>

<!-- Content -->
<table id="mytable" cellspacing="0">
<tr>
<th scope="col" abbr="Configurations">Sequence</th>
<th scope="col" abbr="Configurations">UserName</th>
<th scope="col" abbr="Configurations">DleteUser?</th>
</tr>
<tbody id="tbody1">
</tbody>
</table>
<!-- END Content -->
<div id="footer"></div>
<div><button onclick="AddNewUser()">Add a new User</button></div>
</div>
<!-- END Container -->



</body></html>

阅读 4.4k
4 个回答

试了一下楼上两位大神的说法,貌似都没有什么关系,在我看来

if data == ""
        return;

这里能这么写吗- -||
写了不会报错吗,反正我把这段去了,函数就可以执行了

onclick="AddNewUser"

把括号去掉

另外把script标签放在 body 最下面

不用去括号,就把js的代码拿到下面

Iskrat说的对,js不加圆括号无法判断条件,写成if (data == '') return就可以运行了

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题