【问题标题】:Asp.net Checkbox OnCheckedChanged not fired when JQuery checks check box当 JQuery 检查复选框时,Asp.net Checkbox OnCheckedChanged 未触发
【发布时间】:2011-08-25 04:24:32
【问题描述】:

感谢您抽出宝贵时间阅读和审阅我的帖子。我做了很多谷歌搜索,但还没有找到我的 jquery/asp.net 问题的答案。我的问题是我试图用开/关图像替换标准复选框。有一个关于这样做的简洁教程here。我的问题是,当 Jquery 选中复选框时,没有调用我的 asp OnCheckedChanged 复选框事件。

有效的 ASP.NET 母版页脚本

<link href="~/Styles/jquery.tzCheckbox.css" rel="stylesheet" type="text/css" />
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.min.js" type="text/javascript"></script>
<script type="text/javascript" src="<%#ResolveUrl("~/Scripts/jquery.tzCheckbox.js") %>"></script>
<script type="text/javascript" src="<%#ResolveUrl("~/Scripts/script.js") %>"></script>

带复选框的 Aspx 页面

<asp:Content ID="Content1" ContentPlaceHolderID="HeadContent" runat="server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<asp:DetailsView ID="DetailsView1" runat="server" Height="16px" Width="575px">
    <Fields>
        <asp:TemplateField>
            <EditItemTemplate>
                <asp:CheckBox ID="Edit_CheckBox1_CB" runat="server" AutoPostBack="true" OnCheckedChanged="Edit_CheckBox1_CB_Clicked" />
            </EditItemTemplate>
            <InsertItemTemplate>
                <asp:CheckBox ID="CheckBox1" runat="server" />
            </InsertItemTemplate>
            <ItemTemplate>
                <asp:CheckBox ID="CheckBox1" runat="server" />
            </ItemTemplate>
        </asp:TemplateField>
    </Fields>
</asp:DetailsView>

名为 script.js 的 JQuery 脚本

(function ($) {
$.fn.tzCheckbox = function (options) {

    // Default On / Off labels:

    options = $.extend({
        labels: ['ON', 'OFF']
    }, options);

    return this.each(function () {
        var originalCheckBox = $(this),
            labels = [];

        // Checking for the data-on / data-off HTML5 data attributes:
        if (originalCheckBox.data('on')) {
            labels[0] = originalCheckBox.data('on');
            labels[1] = originalCheckBox.data('off');
        }
        else labels = options.labels;

        // Creating the new checkbox markup:
        var checkBox = $('<span>', {
            className: 'tzCheckBox ' + (this.checked ? 'checked' : ''),
            html: '<span class="tzCBContent">' + labels[this.checked ? 0 : 1] +
                    '</span><span class="tzCBPart"></span>'
        });

        // Inserting the new checkbox, and hiding the original:
        checkBox.insertAfter(originalCheckBox.hide());

        checkBox.click(function () {
            checkBox.toggleClass('checked');

            var isChecked = checkBox.hasClass('checked');

            // Synchronizing the original checkbox:
            originalCheckBox.attr('checked', isChecked);
            checkBox.find('.tzCBContent').html(labels[isChecked ? 0 : 1]);
            if (isChecked) {
               originalCheckBox.attr('checked', isChecked);
            }

        });

        // Listening for changes on the original and affecting the new one:
        originalCheckBox.bind('change', function () {
            checkBox.click();
        });
    });
};
})(jQuery);

CSS 文件

.tzCheckBox{
background:url("../Images/background01.png") no-repeat right bottom;
display:inline-block;
min-width:60px;
height:33px;
white-space:nowrap;
position:relative;
cursor:pointer;
margin-left:14px;
}

.tzCheckBox.checked{
background-position:top left;
margin:0 14px 0 0;
}

.tzCheckBox .tzCBContent{
color: white;
line-height: 31px;
padding-right: 38px;
text-align: right;
}

.tzCheckBox.checked .tzCBContent{
text-align:left;
padding:0 0 0 38px;
}

.tzCBPart
{ 
    background:url("../images/background01.png") no-repeat left bottom;
width:14px;
position:absolute;
top:0;
left:-14px;
height:33px;
overflow: hidden;
}

 .tzCheckBox.checked .tzCBPart{
background-position:top right;
left:auto;
right:-14px;
}

最后一点

一切都按需要工作,当 JQuery 选中复选框时,我只是看不到让页面自动发布。再次感谢您的帮助!

【问题讨论】:

    标签: c# jquery asp.net events checkbox


    【解决方案1】:

    通过 javascript 进行的更改不会触发 onclickonchange 等事件。您需要做的是,一旦您更改了checked 的值,您自己就会触发该事件。我不熟悉.net 的OnCheckedChanged 的细节,但我们假设它在html 中变成onchange(您需要通过查看源代码来验证这一点)。你会做这样的事情:

    originalCheckBox.attr('checked', isChecked);
    originalCheckBox.change(); // will call the onchange code
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-02-21
      • 2019-04-13
      • 2016-04-13
      • 1970-01-01
      • 1970-01-01
      • 2018-06-05
      • 1970-01-01
      • 2013-10-10
      相关资源
      最近更新 更多