ASP.NET表单显示另一个字段中基于节的表单字段

ASP.NET Form Display Form Fields based off section in another field

本文关键字:字段 表单 于节 另一个 NET 显示 ASP      更新时间:2023-09-26

我对ASP.Net开发、MVC 5以及几乎所有Windows都是新手,所以我确信我做错了什么。我在这里四处寻找答案,发现了类似的问题,但我显然做错了。。。可能是因为答案假设我对工作环境有更多的了解…

所发生的情况是,所选的框以"是"开始,我更希望它是"否",并且不管怎样,字段都会显示出来。

我怀疑我的javascript放错了位置和/或丢失了一些重要的东西。

我有一个代码:

<p>
Are you a Licensee?
@Html.DropDownListFor(x => x.Licensee, new[] {
        new SelectListItem() {Text="Yes", Value = bool.TrueString},
        new SelectListItem() {Text="No", Value = bool.TrueString} }, new {id = "Licensee"})
@section scripts{ <script type="text/javascript">
    $(function ()
    {
        $('#Licensee').change(function ()
        {
            var value = $(this).val();
            if (value == true)
            {
                $('#LicName').show();
                $('#LicUrl').show();
                $('#LicRole').show();
            }
            else
            {
                $('#LicName').hide();
                $('#LicUrl').hide();
                $('#LicRole').hide();
            }
        });
    });
    </script> }
    <p>Your Licensee Name: @Html.TextBoxFor(x => x.LicenseeName, new { id = "LicName" })</p>
    <p>Your Licensee Url: @Html.TextBoxFor(x => x.LicenseURL, new { id = "LicUrl" })</p>
    <p>your LIcensee Role: @Html.TextBoxFor(x => x.LicenseRole, new { id = "LicRole" })</p>
</p>

更改

@Html.DropDownListFor(x => x.Licensee, new[] {
        new SelectListItem() {Text="Yes", Value = bool.TrueString},
        new SelectListItem() {Text="No", Value = bool.TrueString} }, new {id = "Licensee"})

@Html.DropDownListFor(x => x.Licensee, new[] {
        new SelectListItem() {Text="No", Value = bool.TrueString} }, new {id = "Licensee"}),
        new SelectListItem() {Text="Yes", Value = bool.TrueString}

这将有助于首先显示值No。

另外,它们总是显示在JavaScript中的原因是,您为每个值(Yes和No(传递bool.TrueString。对于选项"否",您需要使用bool.FalseString

例如:

new SelectListItem() {Text="No", Value = bool.FalseString} }, new {id = "Licensee"}),

好的,我使用这里提示中的复选框找到了它,特别是这个页面,如果复选框选中,则显示/隐藏div

我把这个放在视图页面的标题部分:

function showMe(box, name) {
    var chboxs = document.getElementsByName(name);
    var vis = "none";
    for (var i = 0; i < chboxs.length; i++) {
        if (chboxs[i].checked) {
            vis = "block";
            break;
        }
    }
    document.getElementById(box).style.display = vis;
}

这是一段代码:

<p>
    Are you a Licensee?
    @Html.CheckBoxFor(x => x.Licensee, new { onclick="showMe('LicenseeYes', 'Licensee')" } )
    <div id="LicenseeYes", style="display:none">
        <p>Your Licensee Name: @Html.TextBoxFor(x => x.LicenseeName)</p>
        <p>Your Licensee Url: @Html.TextBoxFor(x => x.LicenseURL)</p>
        <p>your LIcensee Role: @Html.TextBoxFor(x => x.LicenseRole)</p>
    </div>
</p>

感谢大家的帮助,它让我走上了正确的道路,给了我正确的搜索起点。

我还将函数更改为,因为循环不是必需的。如果您有多个复选框,希望显示div部分,那么另一个复选框将起作用:

function showMe(box, name) {        
    var chkbox = document.getElementById(name);
    var vis = "none";
    if (chkbox.checked) {
        vis = "block"
    }
    document.getElementById(box).style.display = vis;
}