简体   繁体   English

如何在选择按钮时更改按钮的颜色?

[英]How to change a color of a button when button is selected?

I have a grid of buttons where if the user clicks on a button, the value of the button goes into the readonly textbox. 我有一个按钮网格,如果用户点击按钮,按钮的值将进入只读文本框。 What I want which I can't get working is that I want the selected button from the grid to turn green and all the other unselected buttons to stay the same color as it is. 我想要的是我无法工作的是我希望网格中的所选按钮变为绿色,而所有其他未选择的按钮保持与原样相同的颜色。 The reason for this is that if the user opens up the grid at any time, they can see the button that is currently selected by the change of color on the button. 这样做的原因是,如果用户随时打开网格,他们可以通过按钮上的颜色变化看到当前选择的按钮。 If another button is selected then the previous selected button would turn white and the new selected button would turn green. 如果选择了另一个按钮,则先前选择的按钮将变为白色,新选择的按钮将变为绿色。

Does anyone know how to do this? 有谁知道如何做到这一点?

my current code is in jsfiddle. 我目前的代码是在jsfiddle。 click here 点击这里

Thank you 谢谢

Just add this: 只需添加:

  $(".gridBtns").removeClass("gridBtnsOn");
  $(this).addClass("gridBtnsOn");

http://jsfiddle.net/Ksh59/7/ http://jsfiddle.net/Ksh59/7/

You could do like i did in this fiddle http://jsfiddle.net/nicolapeluchetti/Ksh59/6/ 你可以像我在这个小提琴做的那样做http://jsfiddle.net/nicolapeluchetti/Ksh59/6/

function buttonclick(button)
{

    $('input:button').css('background-color', 'transparent');
    $(button).css('background-color', 'green');
    if (button.className=="gridBtnsOn")
    {
        button.className="gridBtnsOff";
        return false;
    }

    if (button.className=="gridBtnsOff")
    {
        button.className="gridBtnsOn";
        return false;
    }
}

First of all use jQuery subscription to the events. 首先使用jQuery订阅事件。

$(function() {
    $('#showGrid').click(function(e) {
        if ($('#optionTypeTbl').css("display") == "table") {
            $('#optionTypeTbl').hide();
        }
        else {
            $('#optionTypeTbl').css("display", "table");
        }
        e.stopPropagation();
    });

    $("body").click(function() {
        $('#optionTypeTbl').hide();   
    });

    $('#optionTypeTbl input').click(function() {

        // update value
        $('.box INPUT').val($(this).val())
        $('#optionTypeTbl').hide();

        // update ui class
        $('#optionTypeTbl input.gridBtnsOn').removeClass('gridBtnsOn').addClass('gridBtnsOff');
        $(this).addClass('gridBtnsOn');
    });

});

Code: http://jsfiddle.net/Ksh59/13/ 代码: http//jsfiddle.net/Ksh59/13/

PS I've change styles a little bit. PS我有点改变风格。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM