在MVC升级之后,Javascript函数不起作用

时间:2016-01-04 08:53:39

标签: javascript c# jquery asp.net-mvc asp.net-mvc-4

我已将MVC 3的解决方案升级到MVC 4.

我有2个特定的JavaScript函数,在升级后不再起作用。

  1. 标签
  2. 这是标签现在呈现的方式

    Tab Rendering

    我怀疑它与新版本框架的JavaScript版本有关吗?我不确定。

    代码:

    <script type="text/javascript">
    $(function () {
        $("#tabs").tabs();
    
        $('.taberize').each(function () {
            var e = $(this);
            var panels = e.parents('.ui-tabs-panel');
            if (panels.length == 0) { return; }
            var tabId = $(panels[0]).attr('id');
            e.attr('href', e.attr('href') + '#' + tabId);
        });
    
        $(".container").each(function (e) {
            var height = $(document).height() - 230;
            $(this).height(height);
        });
    });
    

    <小时/>  2.在行悬停功能

    On-Row-Hover功能不再起作用,我有一个&#34;动作菜单&#34;在我的WebGrid的左侧,在行悬停时,它显示编辑和详细信息菜单等功能...这是在JavaScript中完成的。

    <script type="text/javascript">
    var prevRow = null;
                $('#gridData table tbody tr').not(':first').hover(function()
                {
                    $('#myMenu').hide();
                        if (prevRow != this)
                        {
    
                            if (prevRow != null)
                            {
                                $(prevRow).css("background","");
                                $('.actionButtons', $(prevRow)).hide(); 
                            }                       
    
                            $(this).css("background","#EDEFFF");
                            $('.actionButtons', $(this)).show();
                            prevRow = this;          
                        }
                },
                function()
                {
                        if (!$('#myMenu').is(":visible"))
                        {
                            if (prevRow != null)
                            {
                                $(prevRow).css("background","");
                                $('.actionButtons', $(prevRow)).hide(); 
                                prevRow = null;
                            }
                        }
                });
    
               $(".openmenu").contextMenu({ menu: 'myMenu', leftButton: true }, 
               function(action, el, pos) {
                    contextMenuWork(action, el.parents('tr')[0].id , pos);
    
               });
    
               function contextMenuWork(action, id) {
                switch (action) {
    
                    case "insert":
                        {
                         if($.browser.msie&&$.browser.version.substr(0,1)<8){var url='@Url.Action("Create", "Account")';document.location=url}else{CreateNewAccount()}
                          break;
                        }
                    case "createtask":
                        {
                            var url = '@Url.Action("CreateFromAccount", "UserTask")' + '/' + id;
                            document.location = url;
                            break;
                        }
                    case "linkassessment":
                        {
                           var url = '@Url.Action("CreateFromAccount", "Questionnaire")' + '/' + id;
                           document.location = url;
                           break;
                        }
                    case "details":
                        {
                            var url = '@Url.Action("Details", "Account")' + '/' + id;
                            document.location = url;
                            break;
                        }  
                    case "edit":
                        {
                          var url = '@Url.Action("Edit", "Account")' + '/' + id;
                          document.location = url;
                          break;
                        }
                  case "createperson":
                        {
                            if($.browser.msie&&$.browser.version.substr(0,1)<8){var url='@Url.Action("Create", "Person")';document.location=url}else{CreateNewPerson(id)}
                            break;
                        }
                   case "createopportunity":
                        {
                          var url = '@Url.Action("Create", "Opportunity")' + '/' + id;
                          document.location = url;
                          break;
                        }  
    
                 }
    
          }
    
            }); 
    </script>
    
    <div id="gridData">
                <ul id="myMenu" class="contextMenu" style="display: none">
                    <li class="insert"><a href="#insert" id="create">Create New</a></li>
                    <li class="detail"><a href="#details">Details</a></li>
                    <li class="edit"><a href="#edit">Edit</a></li>
                </ul>
    </div>
                <table>
                    <tr>
                        <th class="field-actions-account" style="width: 75px">
                            <a href="#">Actions</a>
                        </th>
    <tr id="@Html.Encode(item.AccountID)">
                            <td>
                            <div class="actionButtons" style="display:none">
                                 <a href="@Url.Action("Edit", new { id = item.AccountID }) " style="text-decoration:none" title="Edit"><img src="@Html.Raw(@Url.Content("~/Content/img/document-pencil-icon.png"))" alt="Edit" title="Edit" style="border:none"/> </a>
                                 <a href="@Url.Action("Details", new { id = item.AccountID }) " style="text-decoration:none" title="Details"><img src="@Html.Raw(@Url.Content("~/Content/img/testDetailsIcon.gif"))" alt="Details" title="Details" style="border:none" /> </a>
                                 <img src="@Html.Raw(@Url.Content("~/Content/img/options.gif"))" alt="More Options" class="openmenu" title="More Options"/>
                            </div>
    

2 个答案:

答案 0 :(得分:1)

将您的javascript代码放在以下部分:

  @section Head
  {
    <script type="text/javascript">
    $(function () {
    $("#tabs").tabs();

    $('.taberize').each(function () {
      var e = $(this);
      var panels = e.parents('.ui-tabs-panel');
      if (panels.length == 0) { return; }
      var tabId = $(panels[0]).attr('id');
      e.attr('href', e.attr('href') + '#' + tabId);
    });

    $(".container").each(function (e) {
       var height = $(document).height() - 230;
       $(this).height(height);
      });
    });
   </script>
  }

然后确保您的“@RenderSection(”Head“,false)位于您的布局中的@ Scripts.Render(”〜/ bundles / jquery“)下面,如下所示:

@Scripts.Render("~/bundles/jquery")
@RenderSection("Head", false)

最后检查你的BundleConfig类有这样的代码:

 public static void RegisterBundles(BundleCollection bundles)
{
  bundles.Add(new ScriptBundle("~/bundles/jquery").Include(
             "~/Scripts/jquery-{version}.js"));
}

答案 1 :(得分:0)

问题是,jquery.validate文件已经被修改,这导致了我的问题。