简体   繁体   English

页面加载时如何根据单选按钮值显示/隐藏 div

[英]How can show/hide div based on radio button value when Page Load

I have this JQuery Code how can i make it to work when form load, currently it only work on radio button click我有这个 JQuery 代码如何在表单加载时使其工作,目前它仅适用于单选按钮单击

I have this radio button我有这个单选按钮

 <div class="m-b">
   <label class="form-check-inline p-l-md m-l-0 m-r-md">
     @Html.RadioButtonFor(model => model.RangeType, "true", new { @name = "projectrange" })
  <div class="check-icon-radio"><i></i></div>Range
    </label>
    <label class="form-check-inline p-l-md m-l-0 m-r-md">
       @Html.RadioButtonFor(model => model.RangeType, "false", new { @name = "projectrange" })
    <div class="check-icon-radio"><i></i></div>Single Code
      </label>
   </div>

I tried this jquery but i doesnt get the radio button value when page load it works great when radio click, How can i make it to get the value of model => model.RangeType, and show/hide the div我试过这个 jquery 但是我在页面加载时没有得到单选按钮值,当单选点击时它工作得很好,我怎样才能让它得到div model => model.RangeType,的值。

  $(window).load(function () {
        $("input[name$='RangeType']").click(function () {
            if ($(this).is(':checked') && $(this).attr("value") == "true") {
                $('#singleCode').hide();
                $('#projectRange').show();
            }
            if ($(this).is(':checked') && $(this).attr("value") == "false") {
                $('#projectRange').hide();
                $('#singleCode').show();
            }
        });
    });

You are just making div show/hide inside onClick handler so it will only get trigger on Click, instead you also need to call same logic on page load您只是在 onClick 处理程序中制作 div 显示/隐藏,因此它只会在单击时触发,相反您还需要在页面加载时调用相同的逻辑
I separated the show/hide logic in a function so you can call it anytime我在 function 中分离了显示/隐藏逻辑,因此您可以随时调用它
I have wrote some dummy code down here try this.我在这里写了一些虚拟代码试试这个。 I have also used else instead of another if or you can use another if also我也使用了 else 而不是另一个if或者你也可以使用另一个 if

$(window).load(function () {
        $("input[name$='RangeType']").click(toggleDiv());

        function toggleDiv(){
            var radio = "input[name$='RangeType']";
            if ($(radio).is(':checked') && $(radio).attr("value") == "true") {
                $('#singleCode').hide();
                $('#projectRange').show();
            }else{
                $('#projectRange').hide();
                $('#singleCode').show();
            }
        }

       toggleDiv();
});

This is a demo worked,and it uses $(function(){}) rather than $(window).load .这是一个演示,它使用$(function(){})而不是$(window).load

View:看法:

<div class="m-b">
    <label class="form-check-inline p-l-md m-l-0 m-r-md">
        @Html.RadioButtonFor(model => model.RangeType, "true", new { @name = "projectrange" })
        <div class="check-icon-radio"><i></i></div>Range
    </label>
    <label class="form-check-inline p-l-md m-l-0 m-r-md">
        @Html.RadioButtonFor(model => model.RangeType, "false", new { @name = "projectrange" })
        <div class="check-icon-radio"><i></i></div>Single Code
    </label>
    <div id="singleCode">
        singleCode
    </div>
    <div id="projectRange">
        projectRange
    </div>
</div>
@section scripts{ 
<script type="text/javascript">
    $(function () {

        var radio = "input[name$='RangeType']";
        console.log($(radio).attr("value"));
        if ($(radio).is(':checked') && $(radio).attr("value") == "true") {
            $('#singleCode').hide();
            $('#projectRange').show();
        }
        if ($(radio).is(':checked') && $(radio).attr("value") == "false") {
            $('#projectRange').hide();
            $('#singleCode').show();
        }
    })
</script>

Controller: Controller:

public IActionResult TestRadioButton() {
        TestRadio test = new TestRadio { RangeType = "true" };
        return View(test);
    }

Result:结果: 在此处输入图像描述

After the page load, if you want to show/hide div based on radio button value, first, you need to get the checked radio button, then, based on the value show or hide the div.页面加载后,如果要根据单选按钮值显示/隐藏 div,首先需要获取选中的单选按钮,然后根据值显示或隐藏 div。 Try to use the following code:尝试使用以下代码:

<div class="m-b">
    <label class="form-check-inline p-l-md m-l-0 m-r-md">
        @Html.RadioButtonFor(model => model.RangeType, "true", new { @name = "projectrange" })
        <div class="check-icon-radio"><i></i></div>Range
    </label>
    <label class="form-check-inline p-l-md m-l-0 m-r-md">
        @Html.RadioButtonFor(model => model.RangeType, "false", new { @name = "projectrange" })
        <div class="check-icon-radio"><i></i></div>Single Code
    </label>
</div>

<div id="singleCode" >
    singleCode Div
</div>
<div id="projectRange">
    projectRange Div
</div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<script>
    $(window).load(function () {
        $("input[name$='RangeType']").click(function () {
            if ($(this).is(':checked') && $(this).attr("value") == "true") {
                $('#singleCode').hide();
                $('#projectRange').show();
            }
            if ($(this).is(':checked') && $(this).attr("value") == "false") {
                $('#projectRange').hide();
                $('#singleCode').show();
            }
        });

        //after page load, based on the checked radio button to show/hide div.
        if ($("input[name$='RangeType']:checked").val() =="true") {
            $('#singleCode').hide();
            $('#projectRange').show();
        } else {
            $('#projectRange').hide();
            $('#singleCode').show();
        }
    });
</script>

The output: output:

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM