0

我正在尝试实现MiniColors。MiniColors 工作正常,但是当 MiniColors 更改时,我无法更新变量 colorSet 的值。我有一个 txt 输入 #colour 显示默认值,但该值没有更新......

<script type="text/javascript">
    $(document).ready(function() {

        $('#picker').minicolors({
            opacity: false,
            defaultValue: '#2083fd',
            control: 'hue',
            textfield: false,
            change: function(hex, opacity) {
                $('#picker').val(hex); 
             }
        });

        var colourSet = $('#picker').val();
        $('#colour').val(colourSet);

任何帮助表示赞赏!

4

1 回答 1

1

If you want colourSet to update every time the color is changed, the assignment needs to go inside the change event function. Right now you have it setting colourSet once, after the minicolor is initiated. I think you want something like the code below. Also, you can probably get rid of the variable and set the #colour element directly, if desired.

$(document).ready(function() {
    var colourSet;

    $('#picker').minicolors({
        opacity: false,
        defaultValue: '#2083fd',
        control: 'hue',
        textfield: false,
        change: function(hex, opacity) {
            $('#picker').val(hex);
            colourSet = hex;
            $('#colour').val(colourSet);
         }
    });
于 2013-03-25T05:25:09.867 回答