【问题标题】:Javascript not working on asp:Datalist checkbox onchangeJavascript 在 asp 上不起作用:Datalist 复选框 onchange
【发布时间】:2017-10-10 00:37:15
【问题描述】:

这是我代码中的数据列表。

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

问题在于,当这个 apsx 页面以 html 呈现时,onchange="return checkconfirm(this);" 出现在跨度而不是复选框上。我尝试了几种确认方法,但我无法将 onchange 事件放在复选框上。

【问题讨论】:

  • 尝试将onchange更改为OnClick

标签: javascript html asp.net checkbox


【解决方案1】:

您可以为此使用 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>

【讨论】:

    猜你喜欢
    • 2016-05-28
    • 1970-01-01
    • 1970-01-01
    • 2021-08-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-03
    • 2011-09-15
    相关资源
    最近更新 更多