根据数组中的值显示/隐藏复选框

Show/Hide checkboxes based on values in an array

本文关键字:显示 隐藏 复选框 数组      更新时间:2023-09-26

Angular和javascript新手,两天来一直困扰着我的大脑。我有一个html表格,我需要在其中显示和隐藏复选框。每行有5个复选框。可见的内容取决于FieldName和FieldName数组中的索引。我在下面对代码进行了评论,希望它能解释我所尝试的内容。

html页面上的角度代码看起来是这样的,数字范围从0到4。——ng show="显示复选框(info.FieldName,0)"

javascript看起来像--

$scope.ShowCheckbox = function (FieldName, index) {       
// FieldName is "MandatedMaterials", "AgencyAgmt", etc.
    var MandatedMaterials = [true, true, false, false, false];
    var AgencyAgmt = [true, true, false, false, false];
    return FieldName[index];  
    // This is what I really really want to work.
    // Only MandatedMaterials[2] and AgencyAgmt[3] return false
    // My experiments to narrow down the problem.
    //if (FieldName == "MandatedMaterials")          
    // works, so FieldName is coming from index.cshtml
    //if (index == 3)
    // works, same thing as above
    //if (MandatedMaterials[0] == true)              
    // works, in that it showed all checkboxes
    //if (AgencyAgmt[0] == true)                     
    // works, same as above
    //if (FieldName[0] == true)                      
    // NOPE -- should show all checkboxes, but instead none
    //if (FieldName[0])                              
    // works, in that it shows all checkboxes
// For testing purposes I had "if" code to return true or false for the 
// commented out statements above. This meant those bits of code would 
// show all the checkboxes.
}

如果你能帮我把合适的复选框显示出来并隐藏起来,我将不胜感激。谢谢。

我不确定这会有帮助,但这正是你想要的。我认为你可以把它作为你想要做的事情的基础

http://www.w3schools.com/angular/tryit.asp?filename=try_ng_events_hide

它来自此页面
http://www.w3schools.com/angular/angular_events.asp

您不能传入字符串,然后将is用作数组。如果您通过CCD_ 1,事实上,如果返回"AgentyAgmt"的第四个字符,则返回"n"所以你的检查失败了。

你应该这样定义:

var chkBoxValues = {};
chkBoxValues.MandatedMaterials = [true, true, false, false, false];
chkBoxValues.AgencyAgmt = [true, true, false, false, false];
return chkBoxValues[fieldName];

这将是工作。