0

I'm using minicolors color picker for Yii and I want to attach a change event on the color picker input as always:

$('#color_picker').change(function(e) { console.log('It works!') });

But it doesn't work, then I tried:

$('#color_picker').miniColors({change: function(hex, rgb) { console.log('It works!') }});

This doesn't work either. Considering docs I should attach this event on creation, but why? What if I need to attach it after creation, can this be done?

4

1 回答 1

0

{ }您缺少提供的左右括号options,这是官方文档中描述的内容

$(selector).minicolors({
    change: function(hex, opacity) {
        console.log(hex + ' - ' + opacity);
    }
});

编辑(1):

<input id="general_bgColor" type="text">
<script>
    $(function(){
        $('#general_bgColor').miniColors({ change: function(hex, rgb) { console.log('it worked!'); //console.log(hex + ' - ' + rgb); } });
    })
</script>

已编辑 (2):这是您应该使用 Yii 扩展添加的内容

$this->widget('ext.widgets.SMiniColors.SColorPicker', array(
    'id' => 'myInputId',
    'defaultValue'=>'#000000',
    'hidden'=>false, // defaults to false - can be set to hide the textarea with the hex
    'options' => array(
        'change' => 'js:function(hex, rgb) { console.log(hex + " - " + rgb); }'
    ), // jQuery plugin options
    'htmlOptions' => array(

    ), // html attributes
));

注意我的线路选项'change'。我认为您的函数字符串上的单引号会无意中使数组无效。它使选项键“更改”始终具有值“0”

于 2013-08-19T08:30:19.830 回答