jquery 如何在mvc3中处理radiobuttonfor单击或更改事件

4nkexdtk  于 2023-08-04  发布在  jQuery
关注(0)|答案(1)|浏览(113)

我正在创建一个简单的用户注册表。用户可以注册为公司或个人。下面是使用RadioButtonFor html helper的Registration表单的相关视图。我已经定义了在JavaScript中使用的id属性:

@model LayoutTest.ViewModels.ProjectUser
    <script>
    $(function () {
    $("#Class_UserType").change(function(){
        var value = $(this).val();
        if (value == 1) {
            $('#Person').hide();
        }
        else if (value == 2) {
            $('#Company').hide();
        }
      });
    });
    </script>
    @using (Html.BeginForm("Create", "Project", FormMethod.Post, new { enctype = "multipart/form-data" }))
    {
   @Html.ValidationSummary(true)
   <fieldset>
   <div class="new_row">
        <div class="editor-label">
            @Html.LabelFor(model => Model.UserType)
        </div>
        <div class="editor-field">
            <div class="editor-field">
                @Html.RadioButtonFor(model => model.UserType, (int)UserType.Company, new { @id= "Class_UserType" }) Company
                @{Html.ValidateFor(model => model.UserType);}
            </div>
            <div class="editor-field">
                @Html.RadioButtonFor(model => model.UserType, (int)UserType.Individual, new { @id = "Class_UserType" }) Individual 
                @{Html.ValidateFor(model => model.UserType);}
            </div>
        </div>
    </div>
    <div class="Company">
          HTML Tags inside
    </div>
    <div class="Person">
        HTML Tags inside
    </div>

字符串
当正确工作时,此代码应隐藏相应的div Company/Person。我已经看了无数的样品,但不能弄清楚我在这里做错了什么!如有任何帮助,我们将不胜感激。
以下是相关的html输出:

<div class="new_row">
        <div class="editor-label">
            <label for="UserType">*User Type</label>
        </div>
        <div class="editor-field">
            <div class="editor-field">
                <input data-val="true" data-val-number="The field *User Type must be a number." data-val-required="The *User Type field is required." id="Class_UserType" name="UserType" type="radio" value="1" /> Company
            </div>
            <div class="editor-field">
                <input checked="checked" id="Class_UserType" name="UserType" type="radio" value="2" /> Individual 
            </div>
        </div>
    </div>

vc6uscn9

vc6uscn91#

以下是我的诀窍:

$('.UserType').change(function () {
        var value = $(this).filter(':checked').val();
        if (value == "1") {
            $('.Person').hide();
            $('.Company').show(500);
        }
        else if (value == "2") {
            $('.Company').hide();
            $('.Person').show(500);
        }
    });

字符串
首先,我需要在我的html中定义一个通用类:

<div class="editor-field">
                @Html.RadioButtonFor(model => model.UserType, (int)UserType.Company,  new { @id= "Class_UTCompany", @class="UserType" }) Company
                @{Html.ValidateFor(model => model.UserType);}
            </div>
            <div class="editor-field">
                @Html.RadioButtonFor(model => model.UserType, (int)UserType.Individual, new { @id = "Class_UTIndividual", @class="UserType" }) Individual 
                @{Html.ValidateFor(model => model.UserType);}
     </div>


第二,我期待的是int中的值,这导致了错误。我可以通过把调试器;我的JavaScript代码

相关问题