代码之家  ›  专栏  ›  技术社区  ›  griegs

基于csv中的值选中复选框

  •  2
  • griegs  · 技术社区  · 14 年前

    如果csv值为23,56,78,并且有一大堆复选框,其中三个复选框有这些值,有没有一种简单的方法来选择它们?

    我怀疑答案是我需要在我的列表中输入并使用选择器,但有其他方法吗?

    2 回复  |  直到 14 年前
        1
  •  0
  •   Yngve B-Nilsen    14 年前

    看一看 http://docs.jquery.com 然后查找.find()方法使jquery返回一组与表达式匹配的复选框(例如,value=23、56或78),然后选中这些复选框。。

        2
  •  0
  •   Dave Thieben    14 年前

    我可能会为htmlhelper创建一个扩展方法,它包含所有可能值的列表和应该选择的值的列表。

        public static MvcHtmlString CheckBoxList( this HtmlHelper htmlHelper, 
            string baseName, IEnumerable<SelectListItem> allvalues, IEnumerable<string> selectedValues )
        {
            StringBuilder output = new StringBuilder();
            int counter = 0;
            foreach ( var item in allvalues )
            {
                output.AppendLine( htmlHelper.CheckBox( string.Format( "{0}_{1}", baseName, counter++ ), 
                     selectedValues.Contains( item.Value ) ).ToString() );
            }
            return MvcHtmlString.Create( output.ToString() );
        }