2017-08-31 76 views
0

所以我收到的錯誤是當我點擊動態創建的html上的刪除按鈕,這意味着調用底部的remove方法並傳遞參數。對象作爲參數的傳遞是在那裏我遇到的問題..未捕獲的語法錯誤:意外的標識符(Javascript,asp.net mvc,cshtml)

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>" + 

這是我認爲這個問題正在引起:

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

的直接上面的部分是在正在造成錯誤,我認爲我是如何通過調用ARGS(對象)在輸入按鈕背面下方的刪除方法..

   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"); 
    } 

回答

0

在將對象傳遞給我的方法之前,我設法通過使用對象操作來解決問題。 謝謝大家的支持!

0

你串連你的對象轉換成字符串,WH ICH可能給你的東西一樣

onclick="removeMarkerFromList([Object object])" 

您可以使用一個ID(字符串或數字),而不是事後檢索您的對象:

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

您也可以字符串化的對象:

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

嗨,謝謝你的回覆。 我試圖將對象串化,但由於某種原因它在開始時增加了一個空格?我試圖刪除它使用一個子字符串,但只是打破了它..任何建議? –

+0

此外,我試圖只傳遞一個對象的內容作爲字符串文字。而且這個工作沒有錯誤,所以我覺得如果可能的話我需要以相似的方式傳遞對象? 感謝您的協助,我的JavaScript並不像我希望它那樣強大 –

相關問題