mardi 2 juin 2015

Why is this jQuery not working on my Sharepoint page?

I have this code at the bottom of my Sharepoint WebPart's .ascx file:

<script>
$(document).ready(function () {
    alert("The ready function has been reached");
    $('#ckbxEmp').on("change", function () {
        if ($('#ckbxEmp').prop("checked")) {
            alert("Checked");
        } else {
            alert("Not checked");
        }
    });
});
</script>

On running the WebPart (opening a page that has the WebPart embedded in it), I see the "The ready function has been reached" alert, but on clicking the checkbox, there is no alert.

The checkbox is created like so in the corresponding .ascx.cs file:

var ckbxEmployeeQ = new CheckBox
{
    CssClass = "finaff-webform-field-input",
    ID = "ckbxEmp"
};

It works in a jsfiddle here:

I also tried changing the code in the .ascx file to this:

<script>
$(document).ready(function () {
    alert("The ready function has been reached");
});

$('#ckbxEmp').on("change", function () {
    if ($('#ckbxEmp').prop("checked")) {
        alert("Checked");
    } else {
        alert("Not checked");
    }
});
</script>

...thinking that maybe I needed to make the event handler separate from the "ready" function, but it makes no difference - still doesn't display any response to a change/click of the check box.

Why not?




Aucun commentaire:

Enregistrer un commentaire