鍍金池/ 問答/HTML/ js函數(shù)未定義是怎么回事,麻煩各位看看呀、

js函數(shù)未定義是怎么回事,麻煩各位看看呀、

**每次都是這個錯誤提示:
(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("當前用戶已經(jīng)加入了名單了,請重試");
        }

        $.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>

回答
編輯回答
奧特蛋

Iskrat說的對,js不加圓括號無法判斷條件,寫成if (data == '') return就可以運行了

2017年9月25日 04:52
編輯回答
情已空

onclick="AddNewUser"

把括號去掉

另外把script標簽放在 body 最下面

2017年7月11日 19:43
編輯回答
壞脾滊

不用去括號,就把js的代碼拿到下面

2017年4月15日 10:02
編輯回答
黑與白

試了一下樓上兩位大神的說法,貌似都沒有什么關(guān)系,在我看來

if data == ""
        return;

這里能這么寫嗎- -||
寫了不會報錯嗎,反正我把這段去了,函數(shù)就可以執(zhí)行了

2018年2月12日 22:08