如何使用jQuery更改选择列表中的选定项目的文本

时间:2011-07-01 20:32:27

标签: jquery

在下面的脚本中,“my_theme”元素是目录列表的选择列表。

当触发“change”事件时,将调用jquery并触发“get”以查明所选文件夹是否具有style.css文件。 get返回true / false。

一切正常。我只是想确定如何根据get的结果来装饰选择列表中的项目。

例如,如果为false,我想将所选项目的背景设为灰色,并将一些文本附加到选项名称标签。

因此,如果列表中当前选定的项目是“alpha”,并且$ .get返回false,则文本应变为“alpha(非活动)”。

$('#my_theme').change
(
    function() 
    {
    //check if this folder has a style.css file
    $.get('<?php echo get_bloginfo('template_directory') ?>/getStyle.php', {theme: myImage}, function(data){doActive(data);});
    }
);

function doActive(data){
if(active){
    //if the stylesheet is missing, append the label with " (inactive)"
    $('#my_theme :selected').text() = $('#my_theme :selected').attr('value') + ' (inactive)';
    }

选项的“value”属性与其标签相同。     }

2 个答案:

答案 0 :(得分:1)

$('#my_theme :selected').each(function(){
    $(this).html($(this).html() + ' (inactive)').css('background-color','gray');
});

答案 1 :(得分:1)

function doActive(data){
if(active){
    //if the stylesheet is missing, append the label with " (inactive)"
    $('#my_theme :selected').text($('#my_theme :selected').attr('value') + ' (inactive)');
    }
相关问题