当 JQuery 检查复选框时,Asp.net 复选框 OnCheckedChanged 不会触发

发布于 2024-11-06 22:51:02 字数 4222 浏览 0 评论 0原文

感谢您花时间阅读和评论我的帖子。我已经进行了大量的 Google 搜索,但尚未找到我的 jquery/asp.net 问题的答案。我的问题是我试图用开/关图像替换标准复选框。 此处有一个关于执行此操作的简洁教程。我的问题是,当 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>

与复选框配合

<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 选中复选框时自动发布页面。再次感谢您的帮助!

Thank you for taking the time to read and review my post. I've done a lot of Google searching but have yet to find the answer to my jquery/asp.net question. My issue is I'm trying to replace the standard check box with an on/off image. There is a neat tutorial about doing this here. My issue is that my asp OnCheckedChanged check box event is not being called when Jquery places a check in the check box.

ASP.NET Master Page Scripts that works

<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 Page with Checkbox

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

JQuery Script called script.js

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

.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;
}

Last Note

Everything works as needed, I just can't seen to get the page to auto post when JQuery checks the check box. Thank you again for your help!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

俯瞰星空 2024-11-13 22:51:02

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

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

Changes made through javascript will not fire events such as onclick or onchange. What you will need to do is fire the event yourself once you change the value of checked. I am not familiar with the particulars of .net's OnCheckedChanged, but let's assume in the html it turns into onchange (you will need to verify this through viewing the source). You would do something like this:

originalCheckBox.attr('checked', isChecked);
originalCheckBox.change(); // will call the onchange code
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文