Using jQuery trying to change background-color of text box but not working in wordpress

旧巷老猫 提交于 2019-12-02 11:39:24

问题


Here is jQuery code which I am using .

<input class="jscolor" name="jscolor" value="F078A0" />
<input name="my_txtbox" type="text" id="my_txtbox_id" value="F078A0" />

<script type="text/javascript">
jQuery('.jscolor').on('change', function($) {
    var color = jQuery('.jscolor').val();
    jQuery('#my_txtbox_id').text(color);
    jQuery('#my_txtbox_id').attr('value',color);
    jQuery('#my_txtbox_id').css('background-color',color);
});
</script>

I put this in my plugin function file #my_txtbox_id get value of color but background-color CSS is not working.

How to solve this?


回答1:


The color code needs a prepending # to be used with CSS.

Change

jQuery('#my_txtbox_id').css('background-color',color);

To

jQuery('#my_txtbox_id').css('background-color', '#' + color);

Edit: You can further optimize the code like so:

jQuery(document).ready(function ($) {
    $('.jscolor').on('change', function (e) {
        var color = $(this).val();
        $('#my_txtbox_id').val(color).css('background-color', '#' + color);
    });
});


来源:https://stackoverflow.com/questions/37629690/using-jquery-trying-to-change-background-color-of-text-box-but-not-working-in-wo

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!