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

为什么这些复选框没有从代码后面进行检查?

  •  0
  • Anders  · 技术社区  · 14 年前

    下面是获取和设置参数的代码:

    <script type="text/C#" runat="server">
        protected string Referrer = string.Empty;
        protected override void OnLoad(EventArgs e)
        {
            try
            {
                Referrer = Request.UrlReferrer.Segments[2].ToLower();
            }
            catch (System.Exception)
            {
    
                Referrer = string.Empty;
            }
            base.OnLoad(e);
        }
    </script>
    

    接下来,这里是复选框的集合:

    <div id="interestSelect1" class="interestPad">
            <%= Html.CheckBoxFor(model => model.Interests[0])%>
            <label for="Interests_0_">Insulation</label>
            <%= Html.CheckBoxFor(model => model.Interests[1])%>
            <label for="Interests_1_">Windows</label>
            <%= Html.CheckBoxFor(model => model.Interests[2])%>
            <label for="Interests_2_">Siding</label>
            <%= Html.CheckBoxFor(model => model.Interests[3])%>
            <label for="Interests_3_">Roofing</label>
        </div>
        <div class="clear">
        </div>
        <div id="interestSelect2" class="interestPad">
            <%= Html.CheckBoxFor(model => model.Interests[4])%>
            <label for="Interests_4_">Gutters/Protection</label>
            <%= Html.CheckBoxFor(model => model.Interests[5])%>
            <label for="Interests_5_">Patio Doors</label>
        </div>
    

    在浏览器中呈现时,以下是每个复选框的外观:

    <input id="Interests_0_" name="Interests[0]" type="checkbox" value="true" />
    <input name="Interests[0]" type="hidden" value="false" />
    <label for="Interests_0_">Insulation</label>
    

    <script type="text/javascript">
        var ref = '<%= Referrer %>';
        $(document).ready(function () {
            switch (ref) {
                case "insulation":
                    $('Interests_0_').attr('checked', 'checked');
                    break;
                case "windows":
                    $('Interests_1_').attr('checked', 'checked');
                    break;
                case "siding":
                    $('Interests_2_').attr('checked', 'checked');
                    break;
                case "roofing":
                    $('Interests_3_').attr('checked', 'checked');
                    break;
                case "gutters":
                    $('Interests_4_').attr('checked', 'checked');
                    break;
                case "patiodoors":
                    $('Interests_5_').attr('checked', 'checked');
                    break;
                default:
                    // do nothing, not valid ref
            }
        });
    </script>
    

    我显然做错了什么,有人能指点我正确的方向吗?

    谢谢!

    编辑: # 标识符。

    2 回复  |  直到 14 年前
        1
  •  1
  •   Nick Craver    14 年前

    #id selectors 需要一个 # 前缀,如下所示:

    $('#Interests_0_').attr('checked', 'checked');
    

    # ,这是一个 element selector <Interests_0_> 元素。

    var ref = '<%= Referrer %>';
    var map = {"insultation":"#Interests_0_",
               "windows":"#Interests_1_",
               "siding":"#Interests_2_",
               "roofing":"#Interests_3_",
               "gutters":"#Interests_4_",
               "patiodoors":"#Interests_5_"};
    $(function () {
       var id = map[ref];
       if(id) $(id).attr('checked', true);
    });
    
        2
  •  1
  •   UpTheCreek    14 年前

    这不是MVC,而是典型的ASP.NET webforms。MVC中没有代码隐藏的概念,通常情况下,您不使用OnLoad之类的事件,也不使用runat=server脚本块。看看 nerd dinner example 有关MVC的概述。