2016-01-21 42 views
0

在某些插件下面的代码运行时一个单选按钮手动检查:触发jQuery代码别处

;(function ($, window, document, undefined) { 
    $.fn.wc_variation_form = function() { 
    var $form = this; 
    $form.on('change', '.variations input:radio', function() { 
    // this code runs when radio buttons are checked 
    }); 
}); 

在我自己的jQuery代码,我需要使用prop('checked', true)检查相同的单选按钮。问题:这样做不能触发上面的代码。我怎样才能确保它被执行?

回答

1

您需要在更改属性时触发事件。以编程方式进行更改时未检测到事件

$(document).on('change', '.variations input:radio', function() { 
    // whatever 
}); 

$('button').click(function(){ 
    $('.variations input:radio[value=foo]') 
     // change the property 
     .prop('checked', true) 
     // now trigger the event 
     .change(); 
}); 
+0

谢谢,虽然似乎不工作。我做了:$('.variations_fieldset input [value =“Every 3 months”]').prop('checked',true).change(); – drake035

+0

在此处正常工作https://jsfiddle.net/o4moed15/ – charlietfl

+0

是的,谢谢@charlietfl!我不明白为什么它不适合我的代码.. – drake035