Yii颜色选择器更改事件问题

Yii color picker change event issue

本文关键字:事件 问题 颜色 选择器 Yii      更新时间:2023-09-26

我正在为Yii使用minicolors颜色选择器,我想一如既往地在颜色选择器输入上附加一个更改事件:

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

但它不起作用,然后我尝试了:

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

这也不起作用。考虑到文档,我应该在创建时附加此事件,但为什么?如果我需要在创作后附上它,这能做到吗?

您缺少提供的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'。我认为函数字符串上的单引号无意中使数组无效。它使选项键"change"始终具有值"0"