Javascript运行时错误:“function”未定义

xvw2m8pv  于 2023-01-24  发布在  Java
关注(0)|答案(3)|浏览(256)

它说我的函数是未定义的,但它在程序内容的主体下面被清楚地定义了。

<button type="button" class="btn btn-primary" data-dismiss="modal" id="CmdBranchEditOk" onclick="CmdBranchEditOk_OnClick()">

    @*Add*@
function CmdBranchAdd_OnClick() {
    alert('Hi');
    @*$('#BranchEdit').modal({
        show: true,
        backdrop: false
    });

    document.getElementById('BranchEdit-BranchCode').value = "";
    document.getElementById('BranchEdit-Branch').value = "";
    document.getElementById('BranchEdit-CompanyID').value = "";*@

}

此时,函数的主体已经被注解掉以测试它是否可以连接到函数.但是当运行并按下时,它返回javarscript运行时错误,说明按钮中的函数未定义.
编辑#1:对不起,复制了错误的代码行xD原谅我

<button style="float:left" id="CmdAddBranch" type="submit" class="btn btn-default" onclick="CmdBranchAdd_OnClick()">Add A Branch</button>

当我在这里的时候,这里是加载的脚本,函数原本应该调用模态,但是现在应该只是报警,但是它没有。

<script src="~/js/jquery.js"></script>
<script src="~/lib/bootstrap/js/bootstrap.js"></script>

<script src="~/wijmo/controls/wijmo.min.js" type="text/javascript"></script>
<script src="~/wijmo/controls/wijmo.input.min.js"></script>
<script src="~/wijmo/controls/wijmo.grid.min.js" type="text/javascript">    </script>
<script src="~/wijmo/controls/wijmo.chart.min.js"></script>
<script src='http://code.jquery.com/jquery-latest.min.js' type='text/javascript'></script>

<link href="~/wijmo/styles/wijmo.min.css" rel="stylesheet" />

编辑#2:下面是整个代码块,看看它是否有任何问题。@{ ViewBag.Title =“分支”;}

<!-- Script Linkings -->
            <script src='http://code.jquery.com/jquery-latest.min.js' type='text/javascript'></script>
            <script src="~/lib/bootstrap/js/bootstrap.js"></script>
            <script src="~/wijmo/controls/wijmo.min.js" type="text/javascript"></script>
            <script src="~/wijmo/controls/wijmo.input.min.js"></script>
            <script src="~/wijmo/controls/wijmo.grid.min.js" type="text/javascript"></script>
            <script src="~/wijmo/controls/wijmo.chart.min.js"></script>
            <link href="~/wijmo/styles/wijmo.min.css" rel="stylesheet" />
            @*<link href="/css/bootstrap.min.css" rel="stylesheet" /> --- this S.O.B will make things white. Big "NO, NO" *@

            @*List*@
            <div id="DivEvents">
                <div class="container">

                    <div class="row">
                        <div class="col-lg-11">
                            <br /><br /><br />
                            <h2 style="margin-bottom:5px; margin-top:5px;">Branches</h2>
                            <button style="float:left" id="CmdAddBranch" type="submit" class="btn btn-default" onclick="CmdBranchAdd_OnClick()">Add A Branch</button>
                        </div>
                    </div>

                    <br />

                    <div class="row">
                        <div class="col-lg-12">
                            <div id="BranchGrid" class="grid"></div>
                        </div>
                    </div>

                    <br />

                    <div class="row">
                        <div class="btn-group col-md-7" id="naviagtionPageEvent">
                            <button type="button" class="btn btn-default" id="btnMoveToFirstPageEvent">
                                <span class="glyphicon glyphicon-fast-backward"></span>
                            </button>
                            <button type="button" class="btn btn-default" id="btnMoveToPreviousPageEvent">
                                <span class="glyphicon glyphicon-step-backward"></span>
                            </button>
                            <button type="button" class="btn btn-default" disabled style="width:100px" id="btnCurrentPageEvent"></button>
                            <button type="button" class="btn btn-default" id="btnMoveToNextPageEvent">
                                <span class="glyphicon glyphicon-step-forward"></span>
                            </button>
                            <button type="button" class="btn btn-default" id="btnMoveToLastPageEvent">
                                <span class="glyphicon glyphicon-fast-forward"></span>
                            </button>
                        </div>
                    </div>

                </div>
            </div>

            @*Edit Detail*@
            <div class="modal fade" id="BranchEdit">
                <div class="modal-dialog">
                    <div class="modal-content">
                        <div class="modal-header">
                            <button type="button" class="close" data-dismiss="modal" aria-hidden="true">
                                &times;
                            </button>
                            <h4 class="modal-title">Branch Edit</h4>
                        </div>
                        <div class="modal-body">
                            <dl class="dl-horizontal">
                                <dt>Branch Code</dt>
                                <dd>
                                    <input class="form-control" id="BranchEdit-BranchCode" type="text" />
                                </dd>
                                <dt>Branch</dt>
                                <dd>
                                    <input class="form-control" id="BranchEdit-Branch" type="text" />
                                </dd>
                                <dt>Company ID</dt>
                                <dd>
                                    <input class="form-control" id="BranchEdit-CompanyID" type="text" />
                                </dd>
                            </dl>
                        </div>
                        <div class="modal-footer">
                            <button type="button" class="btn btn-primary" data-dismiss="modal" id="CmdBranchEditOk" onclick="CmdBranchEditOk_OnClick()">
                                Ok
                            </button>
                            <button type="button" class="btn btn-danger" data-dismiss="modal" id="CmdBranchEditCancel">
                                Cancel
                            </button>
                        </div>
                    </div>
                </div>
            </div>

            @*Loading*@
            <div class="modal fade" id="loading" tabindex="-1" role="dialog" aria-labelledby="Loading..." aria-hidden="true">
                <div class="modal-dialog" style="width: 220px;">
                    <div class="modal-content">
                        <div class="modal-header">
                            <h4 class="modal-title">Loading...</h4>
                        </div>
                        <div class="modal-body">
                            <img src="/img/progress_bar.gif" />
                        </div>
                    </div>
                </div>
            </div>

            @*Module*@
            <script type="text/javascript">
                var Branches;

                var BranchGrid;

                var btnFirstPageEvent;
                var btnPreviousPageEvent;
                var btnNextPageEvent;
                var btnLastPageEvent;
                var btnCurrentPageEvent;

                @*Edit*@
                function CmdBranchEdit_OnClick() {
                    Branches.editItem(Branches.currentItem);

                    $('#BranchEdit').modal({
                        show: true,
                        backdrop: false
                    });

                    var Branch = Branches.currentEditItem;

                    document.getElementById('BranchEdit-BranchCode').value = Branch.BranchCode ? Branch.BranchCode : '';
                    document.getElementById('BranchEdit-Branch').value = Branch.Branch ? Branch.Branch : '';
                    document.getElementById('BranchEdit-CompanyID').value = Branch.CompanyID ? Branch.CompanyID : '';

                }

                @*Add*@
                $(document).ready(function(){
                    function CmdBranchAdd_OnClick() {
                        alert('Hi');
                        @*$('#BranchEdit').modal({
                            show: true,
                            backdrop: false
                        });

                        document.getElementById('BranchEdit-BranchCode').value = "";
                        document.getElementById('BranchEdit-Branch').value = "";
                        document.getElementById('BranchEdit-CompanyID').value = "";*@

                    }   
                }

                @*Delete*@
                    function CmdBranchDelete_OnClick() {

                        Branches.editItem(Branches.currentItem);

                        var Id = Branches.currentEditItem.Id;
                        var BranchDescription = Branches.currentEditItem.Branch;

                        if (confirm("Delete " + BranchDescription + "?") == true) {
                            $.ajax({
                                type: "DELETE",
                                url: "/api/DeleteEvent/" + Id,
                                contentType: "application/json; charset=utf-8",
                                dataType: "json",
                                statusCode: {
                                    200: function () {
                                        window.location.reload();
                                    },
                                    404: function () {
                                        alert("Not found");
                                    },
                                    400: function () {
                                        alert("Bad request");
                                    }
                                }
                            });
                        }
                    }

                    @*Save Edit*@
                    function CmdBranchEditOk_OnClick() {
                        if (confirm("Save Event?") == true) {

                            var Branch = new Object();

                            Branch.BranchCode = document.getElementById('BranchEdit-BranchCode').value;
                            Branch.Branch = document.getElementById('BranchEdit-Branch').value;
                            Branch.CompanyID = document.getElementById('BranchEdit-CompanyID').value;

                            var data = JSON.stringify(Event);

                            // Add New
                            if (Branch.Id == 0) {
                                $.ajax({
                                    type: "POST",
                                    url: "/api/AddEvent",
                                    contentType: "application/json; charset=utf-8",
                                    dataType: "json",
                                    data: data,
                                    success: function (id) {
                                        if (id > 0) {
                                            window.location.reload();
                                        } else {
                                            alert("Not added");
                                        }
                                    }
                                });

                                // Edit
                            } else {
                                $.ajax({
                                    type: "PUT",
                                    url: "/api/UpdateEvent/" + Branch.Id,
                                    contentType: "application/json; charset=utf-8",
                                    dataType: "json",
                                    data: data,
                                    statusCode: {
                                        200: function () {
                                            window.location.reload();
                                        },
                                        404: function () {
                                            alert("Not found");
                                        },
                                        400: function () {
                                            alert("Bad request");
                                        }
                                    }
                                });
                            }
                        }
                    }

                    @*List Functions*@
                    function GetBranches() {
                        var Branches = new wijmo.collections.ObservableArray();
                        $('#loading').modal('show');
                        $.ajax({
                            url: '/api/Event',
                            cache: false,
                            type: 'GET',
                            contentType: 'application/json; charset=utf-8',
                            data: {},
                            success: function (Results) {
                                $('#loading').modal('hide');
                                if (Results.length > 0) {
                                    for (i = 0; i < Results.length; i++) {
                                        Branches.push({
                                            EditId: "<button class='btn btn-primary btn-xs' data-toggle='modal' id='CmdBranchEvent' onclick='CmdBranchEdit_OnClick()'>Edit</button>",
                                            DeleteId: "<button class='btn btn-danger btn-xs' data-toggle='modal' id='CmdBranchEvent' onclick='CmdBranchDelete_OnClick()'>Delete</button>",
                                            Id: Results[i]["Id"],
                                            BranchCode: Results[i]["BranchCode"],
                                            BranchDescription: Results[i]["Branch"],
                                            CompanyID: Results[i]["CompanyID"],
                                        });
                                    }
                                } else {
                                    alert("No data.");
                                }
                            }
                        }).fail(
                            function (xhr, textStatus, err) {
                                alert(err);
                            }
                        );
                        return Branches;
                    }

                    @*Delete*@
                    function DeleteBranch(Id) {
                        $.ajax({
                            type: "DELETE",
                            url: "/api/DeleteEvent/" + Id,
                            contentType: "application/json; charset=utf-8",
                            dataType: "json",
                            data: { Id: BranchId },
                            success: function (response) {
                                alert("Branch Deleted.");
                                window.location.reload();
                            },
                            failure: function (response) {
                                alert("Error - " + response.d);
                            },
                            error: function (e) { }
                        });
                        window.location.reload();
                    }
                    function UpdateNavigateButtonsEvent() {
                        if (Branches.pageSize <= 0) {
                            document.getElementById('naviagtionPageEvent').style.display = 'none';
                            return;
                        }
                        document.getElementById('naviagtionPageEvent').style.display = 'block';
                        if (Branches.pageIndex === 0) {
                            btnFirstPageEvent.setAttribute('disabled', 'disabled');
                            btnPreviousPageEvent.setAttribute('disabled', 'disabled');
                            btnNextPageEvent.removeAttribute('disabled');
                            btnLastPageEvent.removeAttribute('disabled');
                        }
                        else if (Branches.pageIndex === (Branches.pageCount - 1)) {
                            btnFirstPageEvent.removeAttribute('disabled');
                            btnPreviousPageEvent.removeAttribute('disabled');
                            btnLastPageEvent.setAttribute('disabled', 'disabled');
                            btnNextPageEvent.setAttribute('disabled', 'disabled');
                        }
                        else {
                            btnFirstPageEvent.removeAttribute('disabled');
                            btnPreviousPageEvent.removeAttribute('disabled');
                            btnNextPageEvent.removeAttribute('disabled');
                            btnLastPageEvent.removeAttribute('disabled');
                        }
                        btnCurrentPageEvent.innerHTML = (Branches.pageIndex + 1) + ' / ' + Branches.pageCount;
                    }

                    $(document).ready(function () {
                        btnFirstPageEvent = document.getElementById('btnMoveToFirstPageEvent');
                        btnPreviousPageEvent = document.getElementById('btnMoveToPreviousPageEvent');
                        btnNextPageEvent = document.getElementById('btnMoveToNextPageEvent');
                        btnLastPageEvent = document.getElementById('btnMoveToLastPageEvent');
                        btnCurrentPageEvent = document.getElementById('btnCurrentPageEvent');

                        Branches = new wijmo.collections.CollectionView(GetBranches());

                        BranchGrid = new wijmo.grid.FlexGrid('#BranchGrid');

                        BranchGrid.initialize({
                            columns: [
                                        {
                                            "header": "Edit",
                                            "binding": "EditId",
                                            "width": 60,
                                            "allowSorting": false,
                                            "isContentHtml": true
                                        },
                                        {
                                            "header": "Delete",
                                            "binding": "DeleteId",
                                            "width": 60,
                                            "allowSorting": false,
                                            "isContentHtml": true
                                        },
                                        {
                                            "header": "Branch Code",
                                            "binding": "BranchCode",
                                            "allowSorting": false,
                                            "width": "4*"
                                        },
                                        {
                                            "header": "Company ID",
                                            "binding": "CompanyID",
                                            "allowSorting": false,
                                            "width": 80
                                        },
                                        {
                                            "header": "Branch",
                                            "binding": "BranchDescription",
                                            "allowSorting": false,
                                            "width": "4*"
                                        },
                            ],
                            autoGenerateColumns: false,
                            itemsSource: Brances,
                            isReadOnly: true,
                            selectionMode: wijmo.grid.SelectionMode.Row
                        });

                        BranchGrid.trackChanges = true;

                        Branches.pageSize = 15;

                    });

                    UpdateNavigateButtonsEvent();

                    // Page Button Events
                    btnFirstPageEvent.addEventListener('click', function () {
                        Branches.moveToFirstPage();
                        UpdateNavigateButtonsEvent();
                    });
                    btnPreviousPageEvent.addEventListener('click', function () {
                        Branches.moveToPreviousPage();
                        UpdateNavigateButtonsEvent();
                    });
                    btnNextPageEvent.addEventListener('click', function () {
                        Branches.moveToNextPage();
                        UpdateNavigateButtonsEvent();
                    });
                    btnLastPageEvent.addEventListener('click', function () {
                        Branches.moveToLastPage();
                        UpdateNavigateButtonsEvent();
                    });

                });
            </script>

尝试了Rao的建议-仍然没有:(这开始是一个痛苦的屁股..)

aurhwmvo

aurhwmvo1#

您有一个名为的方法的代码

CmdBranchAdd_OnClick

但元素引用了名为

CmdBranchEdit_OnClick
kt06eoxx

kt06eoxx2#

移动这行

<script src='http://code.jquery.com/jquery-latest.min.js' type='text/javascript'></script>

到其他js文件的顶部。
此外,你有两个jqueryidojs文件包括在内。只选择一个是最好的修复您的应用程序。

<script src="~/js/jquery.js"></script>
<script src='http://code.jquery.com/jquery-latest.min.js' type='text/javascript'></script>
mdfafbf1

mdfafbf13#

几点建议:

尝试将函数 Package 在内部,如下所示

$(document).ready(function(){
      function CmdBranchAdd_OnClick() {
         alert('Hi');
      }    
});

那么就像@user86745458说的删除jquery.jsjquery-latest.min.js引用,并将您的js文件放在所有其他链接的顶部!!
尝试
button的类型从submit更改为button

相关问题