Radio Button generates duplicate HTML id-s

前端 未结 3 1821
闹比i
闹比i 2020-12-30 19:41

It seems that the default ASP.NET MVC2 Html helper generates duplicate HTML IDs when using code like this (EditorTemplates/UserType.ascx):

&         


        
3条回答
  •  难免孤独
    2020-12-30 20:08

    If you do need to access the radio buttons via jQuery, I find that often the better place to set the id's will be on the page, close to their intended usage. This is how I did the same:

    @Html.Label(Resource.Question)
    @Html.RadioButtonFor(m => m.Question, true, new {id = "QuestionYes"}) Yes
    @Html.RadioButtonFor(m => m.Question, false, new {id = "QuestionNo"}) No
    

    Then my jQuery:

    if ($("input[id='QuestionNo']").is(":checked")) {
            $('#YesOptionalBlock').removeClass('showDiv');
            $('#YesOptionalBlock').addClass('hideDiv');
    }
    

提交回复
热议问题