1

I have create courses Model to add courses and watch it....there is field in create form named "IsPaid"...it is boolean variable ....when I press on "IsPaid" it leads to open new field named "CourseFees" ... when anyone press "IsPaid" and put fees of course in "CourseFees" field if this one change IsPaid from "true" to "false" the proplem is "CourseFees" field It will not be empty....how to access boolean value in jQuery to make "CourseFees" field empty when "IsPaid" value "false"

here is code in create view:

    <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>

my wrong code in 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>

2 Answers 2

2

You should use the checked property:

<script>
    $(document).ready(function () {
        $("#crsfees").hide();
        $(document).on('change', '#IsPaid', function (e) {
            var selected = this.checked;
            if (selected == false) {
                $("#crsfees").val("0.00");
            }
       });
    });
</script>
Sign up to request clarification or add additional context in comments.

Comments

1

To check whether checkbox is checked, use $('#Whatever_ID').is(':checked')

Checkout this working fiddle

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.