简体   繁体   English

jQuery串联或选择器问题

[英]jQuery Concatenation or selector issue

I have the following: 我有以下几点:

changeDegreeLabel: {
        "Certificate" : "Diploma"
}

this.changeDegreeLabel = function(labelReplacements)
{

this one works 这个作品

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

this one doesn't work yet 这个还没用

        $.each(labelReplacements, function(oldValue, newValue) {
            $("#program_id>optgroup[label="+oldValue+"]")
                .attr('label', newValue);
        });
 }

Basically, I just want to change Certificate to Diploma. 基本上,我只想将证书更改为文凭。 It works in the first function, but not the second. 它在第一个功能中起作用,但在第二个功能中不起作用。 I've tried multiple different quote combinations for concatenating the variable in the attribute selector, but to no avail. 我已经尝试了多种不同的引号组合来连接属性选择器中的变量,但无济于事。 Thoughts? 有什么想法吗?

EDIT: Here's the HTML for the 'this one doesn't work yet' portion 编辑:这是“此功能尚不可用”部分的HTML

<select id="program_id" class="required" name="program_id">
<option value="" selected="selected">— Select —</option>
<optgroup label="Associate">
</optgroup>
<optgroup label="Certificate">
</optgroup>

You need to add the quotes around the value, your string might contain spaces for example. 您需要在值周围加上引号,例如,您的字符串可能包含空格。 You need to use single quotes or escape doubles quotes: 您需要使用单引号或转义双引号:

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

//OR

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

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

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