如何将匿名字符串数组传递给JavaScript函数?

时间:2021-12-07 04:40:04

I want to pass to an array of controls' IDs to a javascript script function so it will switch control's enable state.

我想传递一个控件ID的数组到一个javascript脚本函数,所以它将切换控件的启用状态。

For example, in C# it would be like this:

例如,在C#中它将是这样的:

func(false, new[] { "Control1", "Control2", "Control3" });

In that function I want to find corresponding controls and disable/enable them. For one control I do this next way:

在该功能中,我想找到相应的控件并禁用/启用它们。对于一个控件,我接下来这样做:

<script type="text/javascript" language="javascript">
    function switchControls(value, arr) {
        for (var n = 0; n < array.length; n++)
            document.getElementById([n]).disabled = value;
    }
</script>

<asp:CheckBox runat="server"
     onclick="switchControls(this.checked,
        [
            '<%= Control1.ClientID %>',
            '<%= Control2.ClientID %>'
        ])" 
     Text="Take?" />

How to implement this properly? Have I to use jQuery?

如何正确实现?我要使用jQuery吗?

3 个解决方案

#1


3  

No jQuery (or any other library) necessary.

没有必要的jQuery(或任何其他库)。

It looks like your code will do the trick with a modification or two:

看起来你的代码会通过一两个修改来完成这个工作:

<script type="text/javascript">
    function switchControls(value, arr) {
        for (var n = 0; n < arr.length; n++){
            document.getElementById(arr[n]).disabled = value;
        }
    }
</script>  

As long as your ASP statement passes in a Boolean and an array of IDs (it looks like it does already, but I'm not familiar with ASP), it should work. For example, your onClick could call switchControls like this:

只要您的ASP语句传入一个布尔值和一个ID数组(它看起来已经存在,但我不熟悉ASP),它应该可以工作。例如,你的onClick可以像这样调用switchControls:

switchControls(true, ['foo','bar','baz']);

#2


2  

you don't "HAVE" to use jQuery, but it's somekind cooler to use it :)

你没有“有”使用jQuery,但它使用它更酷一些:)

function checkme(arr){
   if($.isArray(arr)){
     $.each(arr, function(){
       var currentState = this.attr('disabled');
       if(currentState == 'disabled' || currentState == 'true'){
          this.removeAttr('disabled');           
       }
       else{
          this.attr('disabled', 'disabled');
       }
     });
   }
}

usage: checkme([$("#someid"), $("#anotherid"), $("#anotherid")]);

用法:checkme([$(“#someid”),$(“#anotherid”),$(“#anotherid”)]);

#3


0  

<script type="text/javascript" language="javascript">
    function toggleControls(value) {
        $('.toggle').each(function() {
            if (value) {
                $(this).removeAttr('disabled');

            }
            else {
                $(this).attr('disabled', 'true');
            }
        });
    }
</script>

<asp:CheckBox runat="server" onclick="toggleControls(this.checked)" Text="Take?" />
<asp:TextBox runat="server" CssClass="toggle" />

#1


3  

No jQuery (or any other library) necessary.

没有必要的jQuery(或任何其他库)。

It looks like your code will do the trick with a modification or two:

看起来你的代码会通过一两个修改来完成这个工作:

<script type="text/javascript">
    function switchControls(value, arr) {
        for (var n = 0; n < arr.length; n++){
            document.getElementById(arr[n]).disabled = value;
        }
    }
</script>  

As long as your ASP statement passes in a Boolean and an array of IDs (it looks like it does already, but I'm not familiar with ASP), it should work. For example, your onClick could call switchControls like this:

只要您的ASP语句传入一个布尔值和一个ID数组(它看起来已经存在,但我不熟悉ASP),它应该可以工作。例如,你的onClick可以像这样调用switchControls:

switchControls(true, ['foo','bar','baz']);

#2


2  

you don't "HAVE" to use jQuery, but it's somekind cooler to use it :)

你没有“有”使用jQuery,但它使用它更酷一些:)

function checkme(arr){
   if($.isArray(arr)){
     $.each(arr, function(){
       var currentState = this.attr('disabled');
       if(currentState == 'disabled' || currentState == 'true'){
          this.removeAttr('disabled');           
       }
       else{
          this.attr('disabled', 'disabled');
       }
     });
   }
}

usage: checkme([$("#someid"), $("#anotherid"), $("#anotherid")]);

用法:checkme([$(“#someid”),$(“#anotherid”),$(“#anotherid”)]);

#3


0  

<script type="text/javascript" language="javascript">
    function toggleControls(value) {
        $('.toggle').each(function() {
            if (value) {
                $(this).removeAttr('disabled');

            }
            else {
                $(this).attr('disabled', 'true');
            }
        });
    }
</script>

<asp:CheckBox runat="server" onclick="toggleControls(this.checked)" Text="Take?" />
<asp:TextBox runat="server" CssClass="toggle" />