【问题标题】:ASP.NET MVC RAZOR ActionLink clickASP.NET MVC RAZOR ActionLink 点击
【发布时间】:2013-07-15 13:25:38
【问题描述】:

我看到了这个动作链接:

@Html.ActionLink("Edit", "Edit", null, new {@id="editLink", @class="button"})

我想调用一个 java 脚本方法在运行时从我的选定列表框项中生成 URI。

@Html.ListBox("EmployeeName", (IEnumerable<SelectListItem>)ViewBag.selectlist, new { @id = "saglistbox", @class = "SAGListbox" })

作为第一步,当我尝试使用 JavaScript 显示警报时:

<script type="text/javascript">
    $('#editLink').click(function () {
        alert("hello");
    });
</script>

由于某种原因,这没有按预期工作。

【问题讨论】:

    标签: javascript asp.net asp.net-mvc asp.net-mvc-3 asp.net-mvc-4


    【解决方案1】:

    当 DOM 完全加载时,您没有绑定事件。

    <script>
    $(function () {
        $('#editLink').click(function () {
            alert("hello");
    
            //To get selected value use
            var selected = $("#EmployeeName").find(':selected').text();
    
            //To stop default behaviour
            return false;
        });
    });
    </script>
    

    第二个make the URI at runtime from my Selected List Box Item 然后你必须停止它的默认行为,因此使用return false

    【讨论】:

      【解决方案2】:

      试试这个:

      <script type="text/javascript">
          $(document).ready(function()
          {
              $('#editLink').click(function ()
              {
                  alert("hello");
              });
          });
      </script>
      

      【讨论】:

        【解决方案3】:

        您可以传递任何 html 属性作为参数来生成操作链接。你也可以这样做:

        @Html.ActionLink("Edit", "Edit", null, new { @id="editLink", @class="button", onclick = "myfunction()" })
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2014-02-09
          • 2018-06-13
          • 1970-01-01
          • 1970-01-01
          • 2011-01-04
          • 2012-02-13
          • 2011-02-08
          相关资源
          最近更新 更多