Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/383.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
使用MVC3从ActionLink向Javascript函数传递值_Javascript_Asp.net Mvc 3 - Fatal编程技术网

使用MVC3从ActionLink向Javascript函数传递值

使用MVC3从ActionLink向Javascript函数传递值,javascript,asp.net-mvc-3,Javascript,Asp.net Mvc 3,我必须从视图内部调用此javascript函数: $("#addCompositionItem").click(function (carrier) { $.mobile.loading('show'); $.ajax({ url: this.href, cache: false, success: function (html) { $("#Compositi

我必须从视图内部调用此javascript函数:

$("#addCompositionItem").click(function (carrier) {
        $.mobile.loading('show');
        $.ajax({
            url: this.href,
            cache: false,
            success: function (html) {
                $("#Compositions"+carrier).append(html);
                $("#newServicePageContent").trigger("create");
                $.mobile.loading('hide');
            }
        });

        return false;
    });
如您所见,carrier是一个用于在不同容器中加载html部分的参数。如何从操作链接传递此值? 我正在尝试:

@Html.ActionLink("Text", "ActionName", "ControllerName", new { id = "addCompositionItem", carrier="XYZ",type = "submit" })

但是没有成功

我理解所发生的事情的方式是,您正在使用HTML帮助程序生成锚定标记,然后通过JavaScript附加以处理
单击事件。在处理程序中,您希望能够从原始链接获取一段数据

我的建议是使用HTML数据注释来保存数据。现在,您的参数正通过路由参数编码到
href
属性中。如果改为将其移动到html属性并使用
data\u carrier
框架将生成如下所示的锚定标记(不是下划线到连字符的自动转换):

结果应该是:

<a href='...' data-carrier='...'>...</a>

我认为这将涵盖您的用例。

下面的代码片段是Edmassa在《It应该解决您的问题》中给出的

<html>
<head>
<script type="text/javascript">

    // Wait for the page to load first
    window.onload = function() {

      //Get a reference to the link on the page
      // with an id of "mylink"
      var a = document.getElementById("mylink");

      //Set code to run when the link is clicked
      // by assigning a function to "onclick"
      a.onclick = function() {

        // Your code here...

        //If you don't want the link to actually 
        // redirect the browser to another page,
        // "google.com" in our example here, then
        // return false at the end of this block.
        // Note that this also prevents event bubbling,
        // which is probably what we want here, but won't 
        // always be the case.
        return false;
      }
    }
</script>
</head>
<body>
    <a id="mylink" href="http://www.google.com">linky</a>        
</body>
</html>

//等待页面先加载
window.onload=函数(){
//获取对页面上链接的引用
//id为“mylink”
var a=document.getElementById(“mylink”);
//设置单击链接时要运行的代码
//通过将函数分配给“onclick”
a、 onclick=function(){
//你的代码在这里。。。
//如果你不想链接到
//将浏览器重定向到其他页面,
//“google.com”在我们这里的例子中,那么
//在该块末尾返回false。
//请注意,这也可以防止事件冒泡,
//这可能是我们想要的,但不会
//总是这样。
返回false;
}
}
var carrier = $(this).data('carrier');
<html>
<head>
<script type="text/javascript">

    // Wait for the page to load first
    window.onload = function() {

      //Get a reference to the link on the page
      // with an id of "mylink"
      var a = document.getElementById("mylink");

      //Set code to run when the link is clicked
      // by assigning a function to "onclick"
      a.onclick = function() {

        // Your code here...

        //If you don't want the link to actually 
        // redirect the browser to another page,
        // "google.com" in our example here, then
        // return false at the end of this block.
        // Note that this also prevents event bubbling,
        // which is probably what we want here, but won't 
        // always be the case.
        return false;
      }
    }
</script>
</head>
<body>
    <a id="mylink" href="http://www.google.com">linky</a>        
</body>
</html>