twitter-bootstrap Bootstrap 颜色选择器初始颜色值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31698525/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 23:00:32  来源:igfitidea点击:

Bootstrap colorpicker initial color value

javascriptjquerytwitter-bootstrapcolor-picker

提问by New Bee

I am using bootstrap colorpicker but my requirement is to change the default onload color differently on change of select drop down list.

我正在使用引导程序颜色选择器,但我的要求是在更改选择下拉列表时以不同的方式更改默认的加载颜色。

I am using the below constructor :-

我正在使用以下构造函数:-

$('#colorPicker').colorpicker();

This is the code in jsp.

这是jsp中的代码。

<div id='colorPicker'>
 <input type='text' value='#ea0437' class='form-control'/>
 <span class='input-group-addon'></span>
</div>

I am getting the color code from database so I can't add the initial color code as given above in the following line :

我正在从数据库中获取颜色代码,因此我无法在以下行中添加上面给出的初始颜色代码:

<input type='text' value='#ea0437' class='form-control'/>

How can I provide this color in constructor ? The documentation says that there is a color option, but no demonstration given. How can I provide the color initially ? Something like this :

如何在构造函数中提供这种颜色?文档说有一个颜色选项,但没有给出演示。我最初如何提供颜色?像这样的事情:

$('#colorPicker').colorpicker({
color : '#ea0437'
})

Any suggestion? Thanks in advance.

有什么建议吗?提前致谢。

回答by andydoe

I got mine to work after days of trying it out. It appears an onchange event has been attached to the colorpicker input field, but doesn't get fired, after setting the value through script. Hence u have to fire it manually, like this :

经过几天的尝试,我开始工作了。似乎 onchange 事件已附加到颜色选择器输入字段,但在通过脚本设置值后并未触发。因此,您必须手动启动它,如下所示:

$("#inputcolor").val("#bbccff");
$("#inputcolor").trigger('change');

回答by Dileep TP

Use both of these together. It will work

将这两者结合使用。它会工作

    $('#inputcolor').colorpicker({color : "Your_color"})
`   $('#inputcolor').colorpicker('setValue', "Your_color")

回答by ayadi baha

It worked for me with this code :

这段代码对我有用:

$('#colorPicker').colorpicker(document.getElementById('inputcolor'),'#ea0437');

I just gave the input the id of "inputcolor" and then get it with getElementById.

我只是为输入提供了“inputcolor”的 id,然后使用 getElementById 获取它。

EDIT:

编辑:

How about assigning a value to the input when the document loads completely,and then the colorpicker pick it, so it'll be like a default color.

当文档完全加载时为输入分配一个值如何,然后颜色选择器选择它,所以它会像默认颜色一样。

Here's the new code :

这是新代码:

$(document).ready(function(){
    $('#inputcolor').val('#ccc');
});
    $('#colorPicker').colorpicker();

There's so much issues with this library, you can have a look at the issues in their github.

这个库有很多问题,你可以在他们的github 中查看问题。

Here's the new Fiddle

这是新的小提琴

回答by Jonzi

Acording to documentation

根据文档

$colorpicker.colorpicker('setValue', value);

Tried with hex, seems to be working fine

用十六进制试过,似乎工作正常