jquery选项文本返回空

时间:2012-12-12 15:18:56

标签: jquery option

在jquery中从1.7版到最新版

$('option:selected[text~="some text"]')

返回空数组。

在以前的版本中,它的工作效果很好。

为什么呢?错误?

更新:html:

<select class='txt :required' name='terms' size="30" style="width:277px"
    onchange="reset_cost()" id="terms">
  <option label="авто/мото" value="1">авто/мото</option>
  <option label="бизнес/финансы" value="16">бизнес/финансы</option>
  ....
</select>

2 个答案:

答案 0 :(得分:2)

[]语法用于选择属性。 text不是属性。要根据元素的文本进行选择,请使用:contains

$('option:selected:contains("some text")')

由于您正在使用~=,它在字边界之间查看,您可能想要更改选择器(请注意some text之前的空格)

$('option:selected:contains(" some text")')

答案 1 :(得分:2)

根据您发布的HTML,您需要将text更改为label

$('option:selected[label~="some text"]')