Javascript-форум (https://javascript.ru/forum/)
-   jQuery (https://javascript.ru/forum/jquery/)
-   -   Получить значение Colorpicker (https://javascript.ru/forum/jquery/57242-poluchit-znachenie-colorpicker.html)

BMW M6 26.07.2015 12:30

Получить значение Colorpicker
 
Доброго времени суток :)
Использую библиотеку Bootstrap Colorpicker 2.2!
Необходимо получить значение поля, если делать:
var color_tag = $("#color_tag").val();

то, получаю стандартное значение, так как value не меняется!
Как получить код цвета, чтобы отправить его в ajax?

laimas 26.07.2015 13:00

А документация о чем гласит?

.colorpicker('getValue', defaultValue)

Gets the value from the input or the data attribute (if has no input), otherwise returns the default value, which defaults to #000000 if not specified.

BMW M6 26.07.2015 17:25

Я не очень знаю JS, в документации я это видел, но не пойму как использовать это :)

рони 26.07.2015 22:42

BMW M6,
var color_tag = $("#color_tag").colorpicker('getValue')

BMW M6 26.07.2015 23:19

Цитата:

Сообщение от рони (Сообщение 381614)
BMW M6,
var color_tag = $("#color_tag").colorpicker('getValue')

Что-то не работает.

BMW M6 26.07.2015 23:22

Код поля формы:
<div id="color_tag_form" class="form-group color_tag">
                            <label class="col-md-4 control-label">Цвет приставки*</label>
                            <div class="col-md-4">
                                <div class="input-group">
                                    <input id="color_tag" value="#ff001f" type="text" class="form-control">
                                    <div class="input-group-addon"><i></i></div>
                                </div>
                            </div>
                        </div>

И такой JS код:
$('#color_tag').change(function(){
    var color_tag = $("#color_tag").colorpicker('getValue');
    $("#color_tag_form").removeClass("has-success has-error");
    if(!color_tag == '') {
        if(validateHex(color_tag)) {
            $("#color_tag_form").addClass("has-success");
            $('#color_tag_result').text(color_tag);
        } else {
            $("#color_tag_form").addClass("has-error");
        }
    } else {
        $("#color_tag_form").addClass("has-success");
        $('#color_tag_result').text('#ff001f');
    }
});

BMW M6 26.07.2015 23:25

P.S. Ошибка -> Uncaught TypeError: Cannot read property 'getValue' of undefined

рони 27.07.2015 00:03

BMW M6,
а где сначала?
$(function(){
    $('#color_tag').colorpicker();
});

рони 27.07.2015 00:13

BMW M6, условие сами добавьте
$(function(){
    $('#color_tag').colorpicker()
   .on('changeColor.colorpicker', function(event){
    var color_tag = event.color.toHex()
    $("#color_tag_form").addClass("has-success");
    $('#color_tag_result').text(color_tag );

});
});

BMW M6 27.07.2015 00:39

Заменил на ваш код и не стал делать правки, всё ровно не работает.


Часовой пояс GMT +3, время: 16:28.