如何使用 jQuery 访问布尔值

How to access Bool value using jQuery

我已经创建了课程模型来添加课程并观看它....创建表单中有一个名为 "IsPaid" 的字段...它是布尔变量....当我按下 "IsPaid" 它会导致打开名为 "CourseFees" 的新字段 ... 当任何人按 "IsPaid" 并将费用当然放在 "CourseFees" 字段中时,如果此字段将 IsPaid 从 "true" 更改为 "false" 问题是 "CourseFees" 字段它不会为空....如何访问 jQuery 中的布尔值以使 "CourseFees" 字段在 "IsPaid" 值 "false"

这是创建视图中的代码:

    <div class="form-group">
        <div class="checkbox">
            <label>
                <input asp-for="IsPaid" id="ispaid" /> @Html.DisplayNameFor(model => model.IsPaid)
            </label>
        </div>
    </div>
    <div class="form-group" id="crsfees">
        <label asp-for="CourseFees" class="control-label"></label>
        <input value="0.00" palceholder="0.00" asp-for="CourseFees" class="form-control" />
        <span asp-validation-for="CourseFees" class="text-danger"></span>
    </div>

我在 jQuery 中的错误代码:

<script>
    $(document).ready(function () {
        $("#crsfees").hide();
        $(document).on('change', '#IsPaid', function (e) {
            var selected = $(this).val();
            if (selected == false) {
                $("#crsfees").val()=0.00;
            }
       });
    });
</script>

要检查复选框是否被选中,使用$('#Whatever_ID').is(':checked')

检查这个工作fiddle

你应该使用 checked 属性:

<script>
    $(document).ready(function () {
        $("#crsfees").hide();
        $(document).on('change', '#IsPaid', function (e) {
            var selected = this.checked;
            if (selected == false) {
                $("#crsfees").val("0.00");
            }
       });
    });
</script>