1

行。

html:

<div class="selector">
    <select name="something"> //this is what myVar is referring to.
        <option>stuff</option>
    </select>
</div>

假设我有 var:

var myVar = $(".selector").find("[name=something]");

我想与 option:selected 连接:

$(myVar here + " option:selected").change(function(){
    //do something
});

我似乎无法完成这项工作。感谢任何能提供帮助的人。

4

3 回答 3

2

在您已删除的编辑版本中val()myVarjQuery 对象也是如此。你可以做:

myVar.find("option:selected")

或更好

$("option:selected", myVar)
于 2009-10-04T08:05:50.013 回答
0

这应该有效(假设“这里”这个词不在代码中)。name = something 的元素作为值包含什么?您希望使用 jquery 方法获得哪些元素?

于 2009-10-04T08:06:47.750 回答
0

只是选择器方法可能不会只返回一个元素,这可能是使用 jQuery 的更好方法。

 $("select").change(function () {

          $("select option:selected").each(function () {
                // do something
              });

        })
        .trigger('change');
于 2009-10-04T08:09:11.647 回答