Javascript在asp:Datalist复选框onchange上不起作用

Javascript在asp:Datalist复选框onchange上不起作用,javascript,html,asp.net,checkbox,Javascript,Html,Asp.net,Checkbox,这是我代码中的数据列表 <asp:DataList runat="server" ID="dlstate" RepeatColumns="6"> <ItemTemplate> <asp:CheckBox runat="server" ID="chk" Text='<%#Eval("area_state") %>' OnCheckedChanged="chk_state_SelectedIndexChanged" onchang

这是我代码中的数据列表

<asp:DataList runat="server" ID="dlstate" RepeatColumns="6">
     <ItemTemplate>
         <asp:CheckBox runat="server" ID="chk" Text='<%#Eval("area_state") %>' OnCheckedChanged="chk_state_SelectedIndexChanged" onchange="return checkconfirm(this);" AutoPostBack="true" />
     </ItemTemplate>
</asp:DataList>

这是Javascript代码

<script type="text/javascript">
    function checkconfirm(a) {
        if (a.checked == true) {
            return true;
        }
        else {
            debugger;
            var box = confirm("Are you sure you want to unselect this state as areas alloted under this state will be lost?");
            if (box == true)
                return true;
            else {
                a.checked = true;
                return false;
            }
        }
    }
</script>

功能检查确认(a){
如果(a.checked==true){
返回true;
}
否则{
调试器;
var box=confirm(“您确定要取消选择此状态,因为在此状态下分配的区域将丢失吗?”);
如果(框==真)
返回true;
否则{
a、 选中=正确;
返回false;
}
}
}

问题在于,当此apsx页面以html呈现时,
onchange=“return checkconfirm(this);”
显示在span上,而不是复选框上。我已经尝试了几种确认方法,但我无法将onchange事件放在复选框上。

您可以使用jQuery侦听器进行确认

<script type="text/javascript">
    $(document).ready(function () {
        $("#<%= dlstate.ClientID %> input[type='checkbox']").change(function () {
            if (!$(this).prop("checked")) {
                var box = confirm("Are you sure you want to unselect this state as areas alloted under this state will be lost?");
                if (box == true)
                    return true;
                else {
                    $(this).prop("checked", "checked");
                    return false;
                }
            }
        });
    });
</script>

$(文档).ready(函数(){
$(“#输入[type='checkbox']”)。更改(函数(){
如果(!$(此).prop(“选中”)){
var box=confirm(“您确定要取消选择此状态,因为在此状态下分配的区域将丢失吗?”);
如果(框==真)
返回true;
否则{
$(此).prop(“已检查”、“已检查”);
返回false;
}
}
});
});

OnClick