jQuery连接或选择器问题

jQuery Concatenation or selector issue

本文关键字:问题 选择器 连接 jQuery      更新时间:2023-09-26

我有以下内容:

changeDegreeLabel: {
        "Certificate" : "Diploma"
}
this.changeDegreeLabel = function(labelReplacements)
{

this works

        $.each(labelReplacements, function(oldValue, newValue) {
            $('#programList dt:contains(' + oldValue + ')')
                .text(newValue);
        });

这个还不能用

        $.each(labelReplacements, function(oldValue, newValue) {
            $("#program_id>optgroup[label="+oldValue+"]")
                .attr('label', newValue);
        });
 }
基本上,我只是想把证书改成文凭。它在第一个函数中有效,但在第二个函数中无效。我尝试了多种不同的引号组合来连接属性选择器中的变量,但无济于事。想法吗?编辑:这是" this one doesn't work yet "部分的HTML
<select id="program_id" class="required" name="program_id">
<option value="" selected="selected">— Select —</option>
<optgroup label="Associate">
</optgroup>
<optgroup label="Certificate">
</optgroup>

您需要在值周围添加引号,例如您的字符串可能包含空格。您需要使用单引号或转义双引号:

$('#program_id>optgroup[label="'+ oldValue +'"]')
 -^-                          -^-          -^--^-
//OR
$("#program_id>optgroup[label='""+ oldValue +"'"]")
                             -^-             -^-