简体   繁体   English

将js函数添加为url.action中的参数

[英]add js function as parameter in url.action

I want to change the value of a textbox at runtime. 我想在运行时更改文本框的值。

I have this JavaScript function, which returns a textbox value: 我有这个JavaScript函数,它返回一个文本框值:

function getValue(str) {
   var textboxValue = document.getElementById(str).value;
   return textboxValue;
}

This is my link: 这是我的链接:

wg.Column(
        header: " add ", format: @<text>
        <a href="@Url.Action("updateOrder", "MyController", new { VaraId = item.id,  pris = // Here I want to call function getValue("pris") }) ">
      ADD NEW
        </a>
        </text>)
    ))

Keep your anchor tag as clean and set HTML 5 data attributes for your parameter values. 保持锚标记干净,并为参数值设置HTML 5数据属性。

<a class="myLink" href="@Url.Action("updateOrder", "MyController")"
        data-itemid = @item.id data-pris="pris">ADD NEW </a>

Now listen to the click event on this anchor tag and build the target url and naviage to taht. 现在,在此锚标记上侦听click事件,并构建目标url和导航到taht。

$(function(){

  $("a.myLink").click(function(e){
     e.preventDefault();
     var _this=$(this);
     var calculatedVal=getValue(_this.data("pris"));
     var target=_this.attr("href")+"?VaraId="+_this.data("itemid")+
                                                      "&pris="+calculatedVal ;
     window.location.href=target;
  });

});

Assuming you have jQuery loaded to your page. 假设您已将jQuery加载到页面。

If your getValue function is simply returning the value of the element, you may use jQuery selector to get the value. 如果您的getValue函数只是返回元素的值,则可以使用jQuery选择器来获取值。

$(function(){

  $("a.myLink").click(function(e){
     e.preventDefault();
     var _this=$(this);
     var calculatedVal=$("#"+_this.data("pris")).val();
     var target=_this.attr("href")+"?VaraId="+_this.data("itemid")+
                                                       "&pris="+calculatedVal ;
     window.location.href=target;
  });

});

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

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