如何分配ASP。NET隐藏字段值到JavaScript变量?

时间:2022-12-02 11:42:40

Following is the code snippets taken from http://pietschsoft.com/post/2011/09/09/Tag-Editor-Field-using-jQuery-similar-to-*.aspx

下面是来自http://pietschsoft.com/post/2011/09/09/ tag-edi-field - use - jquery -similar to *.aspx的代码片段

// pre-selected tags
values: [
    'javascript',
    'css',
    'jquery'];  

I want to assign values with some hidden field or C# variable, please help as I don't have expertise with JavaScript/jQuery.

我想用隐藏字段或c#变量赋值,请帮忙,因为我没有JavaScript/jQuery的专业知识。

2 个解决方案

#1


3  

You can create a public property and use it in your HTML like the following...

您可以创建一个公共属性,并在HTML中使用它,如下所示……

C# (Added per/comments)

c#(添加/ /注释)

public string Choices { get; set; }

protected void Page_Load(object sender, EventArgs e)
{
    string[] choices = new string[] { "'Choice 1'", "'Choice 2'", "'Choice 3'" };
    Choices = String.Join(",", choices);
}

JavaScript

JavaScript

<script type="text/javascript">
    var values = [<%= Choices %>];
</script>

NOTE: I put single quotes around the values since JavaScript requires the them to recognize the value as part of a string array ( Valid = ['value','value'] / Invalid = [value,value] ).

注意:我在值周围加了单引号,因为JavaScript要求它们将值识别为字符串数组的一部分(Valid = ['value','value'] / Invalid = [value,value])。

#2


0  

hidden:

隐藏:

values: [
        $('hidden1').val(),
        $('hidden2').val(),
        $('hidden3').val()];

or c# (mvc):

或c#(mvc):

values: [
        '@model.var1',
        '@model.var2',
        '@model.var3'];

#1


3  

You can create a public property and use it in your HTML like the following...

您可以创建一个公共属性,并在HTML中使用它,如下所示……

C# (Added per/comments)

c#(添加/ /注释)

public string Choices { get; set; }

protected void Page_Load(object sender, EventArgs e)
{
    string[] choices = new string[] { "'Choice 1'", "'Choice 2'", "'Choice 3'" };
    Choices = String.Join(",", choices);
}

JavaScript

JavaScript

<script type="text/javascript">
    var values = [<%= Choices %>];
</script>

NOTE: I put single quotes around the values since JavaScript requires the them to recognize the value as part of a string array ( Valid = ['value','value'] / Invalid = [value,value] ).

注意:我在值周围加了单引号,因为JavaScript要求它们将值识别为字符串数组的一部分(Valid = ['value','value'] / Invalid = [value,value])。

#2


0  

hidden:

隐藏:

values: [
        $('hidden1').val(),
        $('hidden2').val(),
        $('hidden3').val()];

or c# (mvc):

或c#(mvc):

values: [
        '@model.var1',
        '@model.var2',
        '@model.var3'];