简体   繁体   English

未捕获的语法错误:意外标识符(Javascript、asp.net mvc、cshtml)

[英]Uncaught syntaxerror: Unexpected Identifier (Javascript, asp.net mvc, cshtml)

So the error I am receiving is when I click the remove button on the dynamically created html, which is meant to call the remove method at the bottom and pass the arguments.所以我收到的错误是当我点击动态创建的 html 上的删除按钮时,这意味着调用底部的 remove 方法并传递参数。 The passing of the object as an argument is where I am running into the problems..将对象作为参数传递是我遇到问题的地方..

function addMarkerToList(args) {
        var object = args;
        var camera = args.id;
        var test = selectedCameras.indexOf(args.id);
        var noOfCamerasAllowed = @Model.usersName.CamerasSelectable;

        if (selectedCameras.length < noOfCamerasAllowed) {
            if (test > -1) {
                alert("Camera already in list");
            } else
            {
                selectedCameras.push(args.id);
                var outputString = "";
                for (i = 0; i<selectedCameras.length; i++) {
                    outputString += selectedCameras[i] + ",";
                }
                //$("#cameraSelectedList").append("<p id=" + args.id + ">" + args.id + "</p>");
                $("#cameraSelectedList").append(
                    "<div id = " + camera + " class=\"col-md-12\">" +
                        "<div class=\"col-lg-3 col-md-4 col-sm-6 col-xs-12 user-item\">" + 
                            "<div class=\"user-container\">" +
                            "<a class=\"user-avatar\"><i class=\"glyphicon glyphicon-facetime-video\" style=\"color: #ed1c24; font-size: 36px;\"></i></a>" +
                            "<p class=\"user-name\">" +
                                "<span>Camera</span>" +

This is where I think the issue is being caused:这就是我认为导致问题的地方:

                                "<input type=\"button\" value=\"Remove\" onclick=\"removeMarkerFromList(" + object + ")\"/>" +
                            "</p>" +
                        "</div>" + 
                    "</div>");

The directly above section is where the error is being caused, I think by how I am passing the args called (object) in the input button back the the remove method below..正上方的部分是导致错误的地方,我认为通过我如何将输入按钮中称为(对象)的 args 传递回下面的 remove 方法..

                if (check === 0) {
                    $("#cameraModelPassThrough").append("<input id=" +
                        camera + ".2" + " class=\"form- control text- box single- line valid hidden\" name=\"selectedCameraList\" placeholder=\"Selected Camera ID\" type=\"text\" value=\"" +
                        outputString +
                        "\" aria-required=\"true\" aria-describedby=\"footageRequest_Incident_Location- error\" aria-invalid=\"false\">");
                    check = 1;
                    lastAddedId = (camera + ".2");
                } else {
                    //alert("This is the last added id: " + lastAddedId);
                    document.getElementById(lastAddedId).remove();
                    $("#cameraModelPassThrough").append("<input id=" +
                        camera + ".2" + " class=\"form- control text- box single- line valid hidden\" name=\"selectedCameraList\" placeholder=\"Selected Camera ID\" type=\"text\" value=\"" +
                        outputString +
                        "\" aria-required=\"true\" aria-describedby=\"footageRequest_Incident_Location- error\" aria-invalid=\"false\">");
                    check = 0;
                    lastAddedId = (camera + ".2");
                }
            }
        } else {
            alert("You have added the maximum number of cameras");
        }
    }

    //Removing objects by right clicking the marker
    function removeMarkerFromList(args) {
        var camera = args.id;
        alert(camera);
        var test = selectedCameras.indexOf(camera);
        if (test > -1) {
            document.getElementById(camera).remove();
            selectedCameras.splice(test, 1);
            alert("Camera removed from list");
        } else {
            alert("Camera not in list");
        }
        var outputString = "";
        for (i = 0; i<selectedCameras.length; i++) {
            outputString += selectedCameras[i] + ",";
        }
        document.getElementById(lastAddedId).remove();
        $("#cameraModelPassThrough").append("<input id=" +
            camera + ".2" + " class=\"form- control text- box single- line valid hidden\" name=\"selectedCameraList\" placeholder=\"Selected Camera ID\" type=\"text\" value=\"" +
            outputString +
            "\" aria-required=\"true\" aria-describedby=\"footageRequest_Incident_Location- error\" aria-invalid=\"false\">");
        check = 1;
        lastAddedId = (camera + ".2");
    }

You're concatenating your object into a string, which probably gives you something like你将你的对象连接成一个字符串,这可能会给你类似的东西

onclick="removeMarkerFromList([Object object])"

You can use an id (string or number) instead and retrieve your object afterwards:您可以改用 id(字符串或数字),然后检索您的对象:

"onclick=\"removeMarkerFromList(" + object.id + ")\"/>"

You can also stringify your object:您还可以将对象字符串化:

"onclick=\"removeMarkerFromList(" + JSON.stringify(object) + ")\"/>"

I managed to solve the issue by using object manipulation before passing the object to my method.在将对象传递给我的方法之前,我设法通过使用对象操作来解决这个问题。 Thank you everyone for your assistance!谢谢大家的帮助!

In ASP.NET/MVC/.NET Core project if you are facing this issue, sometimes this might be due to cache storage refresh not happening.在 ASP.NET/MVC/.NET Core 项目中,如果您遇到此问题,有时这可能是由于缓存存储刷新未发生。 In my instance, I had a Redis server running and it needed a manual restart.在我的例子中,我有一个 Redis 服务器正在运行,它需要手动重启。 Go to Task Manager-> Services tab and see the server you are running and restart it manually( Redis server: Memurai in case if you are using windows).转到任务管理器-> 服务选项卡,查看您正在运行的服务器并手动重新启动它(Redis 服务器:如果您使用的是 Windows,则为 Memurai)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM