颜色选择器更新输入值

时间:2013-02-25 22:40:17

标签: javascript jquery html forms color-picker

好的,我花了一整天的时间在这上面,我错过了一些东西而且无法弄明白。我正在使用这个简单的颜色选择器http://www.dynamicdrive.com/dynamicindex11/colorjack/index.htm

我要做的是更新背景颜色,然后更新字体颜色,让用户看到实时结果,并在文本输入框中自动更新十六进制颜色代码。以下是我的代码。

                            <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
            <html xmlns="http://www.w3.org/1999/xhtml">
            <head>
             <title>Color Picker</title>
             <link href="plugin.css" rel="stylesheet" type="text/css" />
             <script src="plugin.js" type="text/JavaScript"></script>
            </head>
            <body>
            <div id="plugin" onmousedown="HSVslide('drag','plugin',event)" style="TOP: 140px; LEFT: 430px; Z-INDEX: 20;">
             <div id="plugHEX" onmousedown="stop=0; setTimeout('stop=1',100);">F1FFCC</div><div id="plugCLOSE" onmousedown="toggle('plugin')">X</div><br>
             <div id="SV" onmousedown="HSVslide('SVslide','plugin',event)" title="Saturation + Value">
              <div id="SVslide" style="TOP: -4px; LEFT: -4px;"><br /></div>
             </div>
             <form id="H" onmousedown="HSVslide('Hslide','plugin',event)" title="Hue">
              <div id="Hslide" style="TOP: -7px; LEFT: -8px;"><br /></div>
              <div id="Hmodel"></div>
             </form>
            </div>



            <table>
              <tr>
                <td class="formlabel" align="right" valign="top">

                  <!-- color box to show background color and font color -->

                  <div id="currentcolor" style="margin-top:2px;padding:4px 10px 4px 10px;background-color:F1FFCC"><span id="showfontcolor" style="color:">Current Text Color</span></div>

                  <!-- End color box --></td>
                <td valign="top"><table>
                    <tbody>
                      <tr>
                        <td><div class="small">Background Color</div>
                          <input name="colorcode" id="colorcode" value="" maxlength="199" class="textbox" style="width:80px" type="text">
                          <a href="javascript:toggle('plugin','colorcode');">Color Picker</a></td>
                        <td>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</td>
                        <td><div class="small">Text Color</div>
                          <input name="fontcolor" id="fontcolor" value="" maxlength="199" class="textbox" style="width:80px" type="text">
                          <a href="javascript:toggle('plugin','fontcolor');">Color Picker</a></td>
                      </tr>
                    </tbody>
                  </table></td>
              </tr>
            </table>


            <script type="text/javascript">

            //*** CUSTOMIZE mkcolor() function below to perform the desired action when the color picker is being dragged/ used
            //*** Parameter "v" contains the latest color being selected
            function mkColor(v){
            //** In this case, just update DIV with ID="colorbox" so its background color reflects the chosen color

            $S('currentcolor').background='#'+v;
            $('colorcode').value='#'+v;
            }

            loadSV(); updateH('F1FFCC');
            toggle('plugin');

            </script>


            </body>
            </html>

问题在于它不会实时更新输入colorcodefontcolor

1 个答案:

答案 0 :(得分:2)

所以你的javascript有几个问题。下面我修改了你的代码:

function mkColor(v){
                var field2update = $('#divUpdateID')[0].innerHTML;
                if (field2update=='colorcode') {
                    $('#currentcolor').background='#'+v;
                    $('#colorcode').value='#'+v;
                } else if (field2update=='fontcolor') {
                    $('#showfontcolor').color='#'+v;
                    $('#fontcolor').value='#'+v;
                }
            }

你所有的jquery选择器都坏了。另外innerHTML是dom函数而不是jquery函数,因此需要在dom对象而不是jquery对象数组上使用它。

以下是更新这些内容的方法。您将不得不弄清楚将它放在代码中的位置,因为我不清楚它应该去哪里:

$("#currentcolor").html("The value it needs to be updated with!");
$("#colorcode").value = "Same as above!";

以上更新背景,下面将更新您的fontcolor

$("#fontcolor").value = "The value you want!";
$("#currentcolor").css("color", "your text color value here!"); //this line updates the color of the text in the `currentcolor` div;

修改

这是对上述代码的重写,也是对您使用的插件的更改:

更改为插件:

var type_to_update = ''; //this goes in the global namespace,
...
function toggle(v, type){ //changed to accept which input should be updated
    $S(v).display=($S(v).display=='none'?'block':'none'); 
    type_to_update = type; //updating the global variable 
}

更改为您的代码:

function mkColor(v){
    if(type_to_update == 'colorcode' || type_to_update == ''){ //default to changing background color if one of the pickers has not been clicked on.
        $S('currentcolor').background = "#"+v;
        $('colorcode').value = '#'+v;
    }else if(type_to_update == 'fontcolor'){
        $S('currentcolor').css("color", '#'+v);
        $('fontcolor').value = '#'+v;
    }

    loadSV();
    updateH('F1FFCC');
    toggle('plugin');
}