Passing variable from ASP.net to JavaScript

后端 未结 7 1858
清歌不尽
清歌不尽 2020-11-29 05:15

How to pass a variable from asp.net to JavaScript?

相关标签:
7条回答
  • 2020-11-29 06:02
    1. Use javascript tag

      <script>
          var var1 = @var1;  
          var var2 = @var2;  
      </script>
      
    2. Use hidden field

      <input type="hidden" value="@var1" id="h_var1"/>  
      <input type="hidden" value="@var2" id="h_var2" />`
      

    in js

    $(function()
    {
        var var1 = $("#h_var1").val();
        var var2 = $("#h_var2").val();
    }
    

    3.Retrieve data via ajax using json

    var var1;
    var var2;
        $.get(url,function(result)
        {
            var1 = result.var1; var2 = result.var2;
         }
    

    @var syntax depend of your view engine. It maybe <%= Var1 %>

    0 讨论(0)
提交回复
热议问题