简体   繁体   English

选中元素上的单选按钮更改事件

[英]Radio button change event on checked element

I have a table which contains two columns. 我有一个包含两列的表。

Let say First column contains Parent Name, and second child names. 假设第一列包含父名称和第二个子名称。 First parent element is selected by default. 默认情况下选择第一个父元素。 I am hiding children of every parent except first element. 除了第一个元素,我隐藏了每个父母的孩子。

Here is the code which works like Accordion(At a time only one should be visible) : 这是像Accordion一样工作的代码(一次只能看到一个):

$(document).ready(function()  {
    var childs = $('.tableCellWBorderGray > div'); // child divs
    childs.hide();

    $("input:radio[name=attribute]").change(function(){
              var parent = $(this).parent().next(); // In our case, second td
              var kids = parent.children();
              childs.slideUp('slow');
              kids.slideDown('slow');
        });
     // select first element and trigger change event to show childs.  
    $('input:radio[name=attribute]:nth(0)').attr('checked',true).trigger('change');
});

This works fine on every other browser. 这适用于所有其他浏览器。

But in IE with Document Mode: IE7 Standards When I click on first checked elements, the change gets fired. 但在IE中使用Document Mode: IE7 Standards当我点击第一个选中的元素时, change将被触发。 Though this happens only once If I click on checked element. 虽然这只发生一次如果我单击选中的元素。 I want to prevent it. 我想阻止它。

I have looked into Why does the jquery change event not trigger when I set the value of a select using val()? 我已经研究了为什么当我使用val()设置select的值时,jquery更改事件不会触发?

The accepted answer says that 接受的答案说明了这一点

The change event requires an actual browser event initiated by the user instead of via javascript code. 更改事件需要用户发起的实际浏览器事件,而不是通过javascript代码。

Is there any work around ? 有什么工作吗?

Full Example: http://jsfiddle.net/CZZeR/2/ 完整示例: http//jsfiddle.net/CZZeR/2/

You have to override the attr function to fire change event 您必须覆盖attr函数以触发更改事件

Code: 码:

$(function() {
    var $attrFn = $.fn.attr;
    $.fn.extend({
        attr: function() {
            var attrCatch = $attrFn.apply(this, arguments);
            if (arguments.length) {
                $(this).change();
            }
            return attrCatch;
        }
    });
});

Working fiddle 工作小提琴

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM