简体   繁体   English

在Ajax.ActionLink的中间运行javascript函数

[英]Run javascript function IN the middle of Ajax.ActionLink

here is the problem: I have an Ajax.ActionLink() in which I need to provide a value from textbox for routeValues: 这是问题所在:我有一个Ajax.ActionLink(),其中需要从文本框中为routeValues提供一个值:

@Ajax.ActionLink("Purchase Tracking", "StatusOfFactor", "Shopping", new { guid = HERE I WANT A JAVASCRIPT FUNCTION TO RETRIEVE VALUE FROM TEXTBOX } , ajaxOptions: new AjaxOptions { UpdateTargetId = "StatusOfFactor", HttpMethod = "GET", InsertionMode = InsertionMode.Replace }, htmlAttributes: new { @class = "btnpeyment btn btn-xs bg-color-btn" })

how should I implement it? 我应该如何实施?

$('#textBoxID').on('change', function(event){
   var value = $('#textBoxID').val();
   $('#ajaxLinkID').attr('guid', value);
})

Replace the IDs with the ones you are using. 用您正在使用的ID替换ID。

Note: If the textbox was placed on the page via an AJAX call, you will need to use event delegation to make this work, delegate it to the parent container that was originally loaded on the page. 注意:如果文本框是通过AJAX调用放置在页面上的,则您将需要使用事件委托来完成此工作,并将其委托给最初加载到页面上的父容器。

You can replace the Ajax.ActionLink and write some javascript code yourself to achieve what you are after. 您可以替换Ajax.ActionLink并自己编写一些javascript代码以实现所追求的目标。

To start, we will replace the Ajax action link with a normal anchor tag 首先,我们将Ajax操作链接替换为普通锚标签

<a id="purchasTrackingLink" class="btnpeyment"
                    href="@Url.Action("StatusOfFactor","Shopping")">Purchase Tracking</a>
<input id="YourTextBoxId" />

Now wireup the click event on this link, prevent the default behavior, read the value of your textbox and use that to generate the new target url and use that for the ajax call.p 现在连接此链接上的click事件,防止出现默认行为,读取文本框的值并将其用于生成新的目标url,并将其用于ajax调用。

$(function(){

   $("#purchasTrackingLink").click(function(e){

       e.preventDefault();

       var textBoxVal=$("#YourTextBoxId").val();
       var url=$(this).attr("href");
       url=url+"?guid="+textBoxVal;
       //Now make the ajax call
       $.get(url,function(response){
          $("#StatusOfFactor").html(response);
       });

   });

});

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

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