填充javascript var形式的ASP.NET

时间:2019-05-06 01:15:07

标签: javascript asp.net

我想在asp.net中创建一个自动填充文本框。但是,我无法动态填充文本框。任何帮助将不胜感激。

JavaScript代码

   x      y
0  LC    [1,2]
1  ZG    [3]
2  KJ    [4,5]
3  U_FT  [6]

我试图从asp.net填充,但我无法使其在这里起作用。

$( function() {
    var availableTags = [
      "ActionScript",
      "AppleScript",
      "Asp",
      "BASIC",
      "Haskell",
      "Ruby",
      "Scala",
      "Scheme"
    ];
    $( "#tags" ).autocomplete({
      source: availableTags
    });

    $("#disable").click ( function() {

       $( "#tags" ).autocomplete({
        disabled: true
      });

});
    $("#enable").click ( function() {

       $( "#tags" ).autocomplete({
        disabled: false
      });

});

  } );

1 个答案:

答案 0 :(得分:1)

您可以添加脚本标签并在<%=%>中使用变量

<script type="text/javascript">
     var myArray = '<%= jsArray %>';
     console.log(JSON.parse(myArray);
 </script>

在您的.cs源代码中,请注意jsArray之前的

         string[] availableTags = new string[]{
            "Las Vegas",
            "Los Angeles",
            "Tampa",
            "New York",
            "s",
            "sss"
            };
           public string jsArray = string.Empty;
            protected void Page_Load(object sender, EventArgs e)
            {
                if (!IsPostBack)
                {
                JavaScriptSerializer serializer = new 

                JavaScriptSerializer();
                jsArray = serializer.Serialize(availableTags);
             }

    }

更新脚本<​​/ p>

$( function() {
     var myArray = '<%= jsArray %>';
    var availableTags = JSON.parse(myArray);
    $( "#tags" ).autocomplete({
      source: availableTags
    });

    $("#disable").click ( function() {

       $( "#tags" ).autocomplete({
        disabled: true
      });

});
    $("#enable").click ( function() {

       $( "#tags" ).autocomplete({
        disabled: false
      });

});

  } );