0

This should be really simple, but I can't get it to work - I'm trying to disable 2 drop down lists when a user clicks a checkbox.

@Html.DropDownList("StartTime", new List<SelectListItem>
{
    new SelectListItem{ Text="10:00 AM", Value = "10:00 AM" },
    new SelectListItem{ Text="12:00 PM", Value = "12:00 PM" },
    new SelectListItem{ Text="2:00 PM", Value = "2:00 PM" }
}

@Html.DropDownList("EndTime", new List<SelectListItem>
{
    new SelectListItem{ Text="10:00 AM", Value = "10:00 AM" },
    new SelectListItem{ Text="12:00 PM", Value = "12:00 PM" },
    new SelectListItem{ Text="2:00 PM", Value = "2:00 PM" }
}
@Html.CheckBox("chkAllDayEvent", new { @onclick = "AllDayEvent_Checked();" })

<script type='text/javascript'>

function AllDayEvent_Checked() {
    $("#chkAllDayEvent").click(function () {
        $('#EndTime').attr("disabled", $(this).is(':checked'));
        $('#StartTime').attr("disabled", $(this).is(':checked'));
    });
}
</script>

My problem is that when I get to the page, the Javascript doesn't fire until I click the checkbox 3 times - on the 3rd time it disables the text boxes, then works as per normal.

4 Answers 4

4

Try this

$(function(){
    $("#chkAllDayEvent").click(function () {
        $('#EndTime').attr("disabled", $(this).is(':checked'));
        $('#StartTime').attr("disabled", $(this).is(':checked'));
    });
});

@Html.CheckBox("chkAllDayEvent", new { @id = "chkAllDayEvent" })

You don't need AllDayEvent_Checked function. as you are binding it using jquery.

Or

function AllDayEvent_Checked() {
        $('#EndTime').attr("disabled", $(this).is(':checked'));
        $('#StartTime').attr("disabled", $(this).is(':checked'));
}

@Html.CheckBox("chkAllDayEvent", new { "@onclick" = "AllDayEvent_Checked();" })
Sign up to request clarification or add additional context in comments.

1 Comment

@Ant P, true, seen your answer after as soon as I answered.
2

This is because you have a function that assigns a click handler to a checkbox. You then trigger this function when the checkbox is clicked. So, when the checkbox is first clicked, you assign a click handler to that checkbox, rather than firing the click handler, which is what you want.

Try this:

<script type='text/javascript'>
$(document).ready(function () {
    $("#chkAllDayEvent").click(function () {
        $('#EndTime').attr("disabled", $(this).is(':checked'));
        $('#StartTime').attr("disabled", $(this).is(':checked'));
    });
});
</script>
@Html.CheckBox("chkAllDayEvent", new { @id = "chkAllDayEvent" })

Comments

1

Try this

@Html.CheckBox("chkAllDayEvent", new {@onclick = "AllDayEvent_Checked();" })

function AllDayEvent_Checked() {    
    $('#EndTime').attr("disabled", $("#chkAllDayEvent").is(':checked'));
    $('#StartTime').attr("disabled", $("#chkAllDayEvent").is(':checked'));

}

1 Comment

Doesn't work, the "@onclick" gives an error - it shouldn't need to be in quotes. Also, @onclick = "blah" works ok as the onclick event is being rendered in the view, just doesn't seem to work for the first few clicks on the checkox
0
function AllDayEvent_Checked() {
    $('#EndTime').attr("disabled", $(this).is(':checked'));
    $('#StartTime').attr("disabled", $(this).is(':checked'));
}

@Html.CheckBox("chkAllDayEvent", new { "@onclick" = "AllDayEvent_Checked();" })

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.