javascript - 单击标签时,复选框 'change' 事件起作用。在 ie8, ie7 中

标签 javascript jquery html css

在我的表单中,我有一个 CheckBox,并且我有一个 CheckBox 标签。在 firefox 和 IE9 中,CheckBox change 事件 按预期工作,但在 IE8 和 IE7 中,当单击标签而不是复选框时会触发该事件。

我的 HTML

<div class="item-container">
    <div class="label-container">
    </div>
    <div class="textbox-container">
        <input id="AddNewProductCategory" class="" type="checkbox" width="20px" height="20px"
            value="1" name="addnewproductcategory" tabindex="1900" />
    </div>
    <div class="after-checkbox-label">
        <label class="Verdana11-424039" for="AddNewProductCategory">
            Add New Service Category</label>
    </div>
</div>

我的 jQuery

jq('#AddNewProductCategory').change(function(){
    jq('#CategoryName').next('label[for=CategoryName]').remove();
    jq('#Category').next('label[for=Category]').remove();

    if (!jq('#AddNewProductCategory').is(':checked')) {
         alert("in change");
        jq('input[name="newcategory"]').val('');
        jq('#CategoryName').hide();     
        jq('#store_category').hide();
        jq('#Category').removeAttr('disabled');

    }
    else {
        jq('#Category').attr('disabled', 'disabled');
        jq('#CategoryName').show();     
        jq('#store_category').show();
    }
});

最佳答案

仅供将来引用,根据我的内存,“更改”事件在旧版本的 IE 中有点错误。

您必须在 IE 中使用 propertychange 事件才能使其按预期运行。我喜欢使用的代码是:

编辑2(回到核心)

$(element).on(navigator.userAgent.match(/msie/i) ? "propertychange" : "change", function(evt) {
    evt.preventDefault();
    // Your code here
});

编辑 1(有问题,不可靠)

$(element).on(!$.support.changeBubbles ? "propertychange" : "change", function(evt) {
    evt.preventDefault();
    // Your code here
});

此代码已更新为与 jQuery 1.9+ 一起使用,Marius 在评论中提供了一些输入。

原始(已弃用)

这里是那些使用旧版本 jQ 的旧代码:

$(element).bind($.browser.msie ? 'propertychange' : 'change', function(evt) {
    evt.preventDefault();
    // Your code here
});

关于javascript - 单击标签时,复选框 'change' 事件起作用。在 ie8, ie7 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8005442/

相关文章:

javascript - 在 CKEditor 中添加一个 <div> 而不在工具栏上嵌入一个 <div> 按钮

jquery - 内容后带有数字的 CSS 按钮?

javascript - 为任意 html 文档创建不显眼的覆盖层

javascript - 验证复制|克隆的输入字段是否具有相同的信息

javascript - swiper js 每个 View 多张幻灯片

javascript - 我可以在页面加载之前加载灯箱吗

javascript - 设置范围 slider 的最小处理程序值

javascript - 从另一个action获取html代码并通过javascript操作该代码

javascript - jQuery - JavaScript,将参数与 "event"一起传递给函数

javascript - 如何知道javascript中的对象数组中是否有值?