jquery - 如何在 jQuery 中的下拉列表更改时更改字体颜色?

标签 jquery css

我正在尝试更改下拉框中文本的颜色。紧急、重要和正常。紧急应为红色,重要为橙色,正常为绿色。我创建了以下 JavaScript,但除了示例警报之外,其他警报不起作用。颜色也没有改变。

        var d = NWF$('#' + jsPriority + ' option:selected').val();
       //alert(d);
        switch (d)
  {
            case("Urgent - Immediate Attention or Response Required within 2 days"):
                 alert(d);
                 //$(this).find('option:selected').css('color', 'red');
                 NWF$('#'+jsPriority).find('option:selected').css('color', 'red');
                 break;
            //case d.startsWith("Urgent"):            
            //break;
            case ("Important - High Attention or Response Required within 7 days"): 
                NWF$('#'+jsPriority).find('option:selected').css('color', 'orange');                
                break;
            case ("Normal - Review or Response Required within 14 days"): 
                //alert(d);
                NWF$('#'+jsPriority).find('option:selected').css('color', 'green');
                break;            
        }

最佳答案

我终于让它工作了。这是 jquery 代码的工作版本。

// coloring priority
NWF$(document).ready(function() {    
        var d = NWF$('#' + jsPriority + ' option:selected').val();
       //alert(d);
        switch (d)
  {
            case("Urgent - Immediate Attention or Response Required within 2 days"):
                 //alert(d);
                 //$(this).find('option:selected').css('color', 'red');
                 NWF$('#'+jsPriority).find(' option:selected').css('color', 'red');
                 NWF$('#'+jsPriority).css('color', 'red');
                 break;
            //case d.startsWith("Urgent"):            
            //break;
            case ("Important - High Attention or Response Required within 7 days"): 
                NWF$('#'+jsPriority).find(' option:selected').css('color', 'orange'); 
                NWF$('#'+jsPriority).css('color', 'orange');               
                break;
            case ("Normal - Review or Response Required within 14 days"): 
                //alert(d);
                NWF$('#'+jsPriority).find(' option:selected').css('color', 'green');
                NWF$('#'+jsPriority).css('color', 'green');
                break;            
        }   
});


// coloring priority
NWF$(document).ready(function() {    
NWF$('#' + jsPriority).change(function() {
        var d = NWF$('#' + jsPriority + ' option:selected').val();
       //alert(d);
        switch (d)
  {
            case("Urgent - Immediate Attention or Response Required within 2 days"):
                 //alert(d);
                 //$(this).find('option:selected').css('color', 'red');
                 NWF$('#'+jsPriority).find(' option:selected').css('color', 'red');
                 NWF$('#'+jsPriority).css('color', 'red');
                 break;
            //case d.startsWith("Urgent"):            
            //break;
            case ("Important - High Attention or Response Required within 7 days"): 
                NWF$('#'+jsPriority).find(' option:selected').css('color', 'orange'); 
                NWF$('#'+jsPriority).css('color', 'orange');               
                break;
            case ("Normal - Review or Response Required within 14 days"): 
                //alert(d);
                NWF$('#'+jsPriority).find(' option:selected').css('color', 'green');
                NWF$('#'+jsPriority).css('color', 'green');
                break;            
        }   
});
});

关于jquery - 如何在 jQuery 中的下拉列表更改时更改字体颜色?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48269098/

相关文章:

javascript - 如何在 Globalize 中获取工作日?

php - 如何使用ajax更新mysql并在更新成功后重定向页面?

javascript - jQuery ready() 上的 document.readyState

html - 如何从打印页面 (Chrome) 中删除 url(显示在链接后)?

html - bootstrap 3 将文本内容包装在 div 中以进行水平对齐

css - 将 DIVS 放在第一个 DIV 下的 av DIV 中

php - 屏幕上的div定位

asp.net - jQuery,ASP.NET

html - CSS - 目标 div 的最后一个实例

jquery - 在显示动画结束之前获取尺寸信息