简体   繁体   English

Javascript语法错误不确定我在做什么错

[英]Javascript syntax error not sure what I am doing wrong

I'm rather new to JavaScript but I am learning.. But now I am stuck, I made this JavaScript Except I get Syntax error 我对JavaScript不太熟悉,但是我正在学习。.但是现在我被卡住了,我制作了这个JavaScript,但是遇到语法错误

$(function () {
    $(function () {
        $('#date').datepicker({
            showButtonPanel: true,
            changeMonth: true,
            changeYear: true,
            dateFormat: "yy-mm-dd",
            firstDay: 1,
            onSelect: function (dateText) {
                $('#EndDate').datepicker('option', 'minDate', new Date(dateText));
            }
        });
    });
});

if (!((navigator.userAgent.match(/iPhone/i)) || (navigator.userAgent.match(/iPod/i)) || (navigator.userAgent.match(/iPad/i)))) {

    $(function () {
        $("#startTime").timePicker({
            step: 15,
            endTime: "23:45"
        });
    });
    $(function () {
        $("#endTime").timePicker({
            step: 15,
            endTime: "23:45"
        });
    });
    $(function () {
        $("#breakTime").timePicker({
            step: 5,
            endTime: "03:00"
        });
    });
}
function SetDate(dt) {
    $('#date').val(dt);
}

var n = @(Model.Projects.Count);

function AddProject() {
    n++;
    $.ajax({
        type: "GET",
        url: "@(Url.Action("Project"))/" + n + "/?showDescription=false",
        dataType: "html",
        success: function(data) {
            $('#projects').append(data);
        }
    });
}

It is on my AddProject function I get my error, how ever I can't see what could be wrong? 是在我的AddProject函数上,我得到了错误,但是我怎么看不到可能出了什么问题呢?

And this is the view I want to use the script on. 这是我要在其上使用脚本的视图。

                            @if (ViewData["posted"] != null)
                            {
                                <div class="alert alert-success">
                                    <strong>Inställningar sparade</strong>
                                </div>
                            }

                            @using (Html.BeginForm("Settings", "Reports", FormMethod.Post, new { enctype = "multipart/form-data" }))
                            {
                                <div class="portlet light">
                                    <div class="portlet-title">
                                        <div class="caption">
                                            <span class="caption-subject font-green-sharp bold uppercase">Inställngar</span>
                                        </div>
                                    </div>
                                    <table class="table table-striped table-bordered table-hover">
                                        <tr>
                                            <td>Starttid:</td>
                                            <td>@Html.TextBox("startTime", Model.Times.StartTime)</td>
                                        </tr>
                                        <tr>
                                            <td>Sluttid:</td>
                                            <td>@Html.TextBox("endTime", Model.Times.EndTime)</td>
                                        </tr>
                                        <tr>
                                            <td>Rastlängd:</td>
                                            <td>@Html.TextBox("breakTime", Model.Times.BreakTime)</td>
                                        </tr>
                                    </table>
                                </div>

                                        <div id="projects">
                                            @foreach (var data in Model.Projects)
                                            {
                                                Html.RenderPartial("Project", data, ViewData["vd"] as ViewDataDictionary);
                                                var viewDataDictionary = ViewData["vd"] as ViewDataDictionary;
                                                if (viewDataDictionary != null)
                                                {
                                                    viewDataDictionary["id"] = (int)viewDataDictionary["id"] + 1;
                                                }
                                            }
                                        </div>
                                            <a href="javascript:AddProject()" class="btn btn-primary">Lägg till projekt</a>
                                            <button type="submit" class="btn btn-primary">Spara</button>
                            }
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>

Javascript files do not support Razor compilation. Javascript文件不支持Razor编译。 Only views do that. 只有视图可以做到这一点。 You cannot have Razor in separate JS files. 您不能在单独的JS文件中使用Razor。

Instead inject any required information into the view (eg into the elements as data- or other attributes) and have your JS pick up those values from the elements (or from a few global vars you set in the view). 而是将任何必需的信息注入视图(例如,将元素作为data-或其他属性注入到视图中),然后让JS从元素(或从您在视图中设置的一些全局变量)中获取这些值。

I will add some examples for you... 我将为您添加一些示例...

Example HTML (attribute injection): HTML示例(属性注入):

<a id="addProject" href="@(Url.Action("Project") + "/{id}/?showDescription=false")" class="btn btn-primary">Lägg till projekt</a>

JQuery jQuery查询

 $('#addProject').click(function(){
      var url = $(this).attr('href');
      url = url.replace("{id}", myNewId);
      $.ajax({ 
         url: url
         ...
      });
 });

I strongly recommend never using inline event handlers (like onclick="" ) with jQuery as the separate the event code from the registration aking maintenance harder and do not have all the same features as jQuery event handling. 我强烈建议不要将内联事件处理程序(如onclick="" )与jQuery一起使用,因为这样会使事件代码与注册更加分开,从而使维护工作更加困难,并且不具有与jQuery事件处理功能相同的功能。

Example HTML (global var injection) HTML示例(全局var注入)

Place this code snippet in the view 将此代码段放在视图中

<script type="text/javascript>
     var projectCount = @(Model.Projects.Count);
     // or
     window.projectCount = @(Model.Projects.Count);
</script>

Then you can access projectCount from other JS files with: 然后,您可以使用以下命令从其他JS文件访问projectCount:

  var count = projectCount + 1;

or 要么

  var count = window.projectCount + 1;

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

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