Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/85.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
JavaScript代码在第一次加载页面时不会工作,但是当我进行一些分页时,它会工作得很好_Javascript_Jquery_Html_Asp.net Mvc_Razor - Fatal编程技术网

JavaScript代码在第一次加载页面时不会工作,但是当我进行一些分页时,它会工作得很好

JavaScript代码在第一次加载页面时不会工作,但是当我进行一些分页时,它会工作得很好,javascript,jquery,html,asp.net-mvc,razor,Javascript,Jquery,Html,Asp.net Mvc,Razor,我的sp.net mvc web应用程序中有以下索引视图:- @model IPagedList<TMS.Models.VirtualMachine> @{ ViewBag.Title = "Virtual Machine List"; } <h2>Virtual Machine List</h2> <i class=" icon-plus"></i>@Html.ActionLink("Create New", "Creat

我的sp.net mvc web应用程序中有以下索引视图:-

@model IPagedList<TMS.Models.VirtualMachine>

@{
    ViewBag.Title = "Virtual Machine List";
}

<h2>Virtual Machine List</h2>
<i class=" icon-plus"></i>@Html.ActionLink("Create New", "Create") | @Html.ActionLink("Assets","Asset")

//code goes here
@Html.Partial("**_PagedVMTable**",Model)
现在我面临以下问题:-

  • 当页面第一次加载时,当我单击“ServerSort”或“RackSort”时,JavaScript不会启动。但是当我点击一个页面链接时,当点击“ServerSort”或“RackSort”链接时,javaScript就会启动。所以不确定为什么会发生这种行为

  • 谢谢

    哪个部分没有执行?何时创建“服务器排序”和“机架排序”?可能从.click调用切换到$(“#ServerSort”).live(“click”,function(){});你能显示你的按钮是在哪里定义的吗?你能在多个浏览器中重现这个问题吗?(Chrome,IE,FF,Safari)@b是的,这些按钮是在另一个局部视图中定义的,该视图使用以下@Html.partial(“~/Views/VirtualMAchine/_VMTable.cshtml”,Model)在_pagedvmtablepartialview中调用。
    @model IPagedList<TMS.Models.TMSVirtualMachine>
    <div id ="VMTable">
    
                            <div class="pagedList" data-tms-target="#VMTable">
                             @Html.PagedListPager(Model , page => Url.Action("Index","VirtualMachine", new { searchTerm = ViewBag.searchTerm , page,sort = ViewBag.CurrentSortOrder }),
                             PagedListRenderOptions.EnableUnobtrusiveAjaxReplacing(PagedListRenderOptions.ClassicPlusFirstAndLast, new AjaxOptions { UpdateTargetId = "VMTable" , LoadingElementId="progress2" }))
    
                            </div>  
                                 <img src="~/Content/Ajax-loader-bar.gif" class="loadingimage" id="progress2" />
    
                            @Html.Partial("~/Views/VirtualMAchine/_VMTable.cshtml",Model)
                        <img src="~/Content/Ajax-loader-bar.gif" class="loadingimage" id="progress3" />
                                 <div class="pagedList" data-tms-target="#VMTable">
                             @Html.PagedListPager(Model , page => Url.Action("Index","VirtualMachine", new { searchTerm = ViewBag.searchTerm , page,sort = ViewBag.CurrentSortOrder }),
                             PagedListRenderOptions.EnableUnobtrusiveAjaxReplacing(PagedListRenderOptions.ClassicPlusFirstAndLast, new AjaxOptions { UpdateTargetId = "VMTable" , LoadingElementId="progress3"}))
    
                            </div>  
    </div></div></div>
        </div>
    <script type="text/javascript">
            $(document).ready(function () {
    
                $("#ServerSort").click(function () {
    
                    $.ajax({
                        type: "Get",
                        url: "@Url.Action("Index","VirtualMachine")",
    
                        data: { searchTerm:  "@ViewBag.searchTerm.ToString()" , page:"1" , sort: "@ViewBag.ServerSortPam" },
                        //contentType: "application/json; charset=utf-8",
                        //dataType: "json",
                        cache: false,
                        success: successFunc,
                        error: errorFunc
                    });
    
                    function successFunc(data, status) {     
                        $("#VMTable").html(data);
                    }
    
                    function errorFunc() {
                        alert('error');
                    }
                })
                $("#RackSort").click(function () {
    
                    $.ajax({
                        type: "Get",
                        url: "@Url.Action("Index","VirtualMachine")",
    
                        data: { searchTerm: "@ViewBag.searchTerm.ToString()", page: "1", sort: "@ViewBag.RackSortPam" },
                        //contentType: "application/json; charset=utf-8",
                        //dataType: "json",
                        cache: false,
                        success: successFunc,
                        error: errorFunc
                    });
    
            function successFunc(data, status) {
                $("#VMTable").html(data);
            }
    
            function errorFunc() {
                alert('error');
            }
        })
            });
        </script>