How to change input border-color, with jQuery, depending on its value?

后端 未结 3 1997
一生所求
一生所求 2021-02-19 09:25

I have question about jQuery. I have code below which works fine; it changes input border-color depending on its value, which is typed and entered. But

相关标签:
3条回答
  • 2021-02-19 10:03

    Just trigger the event:

    $("input").change(function() {
        // ...
    }).trigger("change");
    

    DEMO: http://jsfiddle.net/9B5SX/1/

    0 讨论(0)
  • 2021-02-19 10:06

    I'd suggest:

    $('input').on('change', function(){
        var v = parseInt(this.value, 10);
        $(this).css('border', function(){
            if (v < 7) {
                return '5px solid #f00';
            }
            else if (v == 7) {
                return '5px solid #0f0';
            }
            else if (v > 7) {
                return '5px solid #f90';
            }
            else {
                return '5px solid #000';
            }
        });
    }).change();
    

    JS Fiddle demo.

    Though honestly, given that every situation seems to require a border-width of 5px and border-style of solid, I'd set those parts in the CSS:

    input {
        border-width: 5px;
        border-style: solid;
    }
    

    And simply update the border-color in the jQuery:

    $('input').on('change', function(){
        var v = parseInt(this.value, 10);
        $(this).css('border-color', function(){
            if (v < 7) {
                return '#f00';
            }
            else if (v == 7) {
                return '#0f0';
            }
            else if (v > 7) {
                return '#f90';
            }
            else {
                return '#000';
            }
        });
    }).change();
    

    JS Fiddle demo.

    And, finally, just because I sometimes can't help myself (and while this approach can be taken, it's not one I can honestly recommend...), with added conditional operators:

    $('input').on('change', function(){
        var v = parseInt(this.value, 10);
        $(this).css('border-color', function(){
            var v = parseInt(this.value,10);
            return isNaN(v) ? '#000' : v < 7 ? '#f00' : v == 7 ? '#0f0' : '#f90';
        });
    }).change();
    

    JS Fiddle demo.

    References:

    • change().
    • on().
    • parseInt().
    0 讨论(0)
  • 2021-02-19 10:07

    Same issue here. On my case, the input was a table column and it was displayed for every table row. With the next code I managed to change only one input color, not all when I changed the value for one.

             $('input[type=number]').each(function () {
    
                 /* Change border color depending on the input value */
                 var value = parseInt(this.value, 10);
                 if (value != 0) {
                     $(this).css('border-color', function () {
                         return value ? 0 : '#bfbfbf', '#f32d83';
    
                     });
    
                 }
                 else {
                     $(this).css('border-color', '#bfbfbf');
                 }
    
             });
    

    Hope it helps

    0 讨论(0)
提交回复
热议问题