如何访问没有ID或类的选择下拉菜单对象?

时间:2014-02-03 00:02:37

标签: javascript jquery

然后告诉选择了哪个selectedIndex?!

这是情况:select对象没有id或class。并且代码的目标是确保用户在“添加到购物车”之前进行选择。此外,这出现在许多不同的产品页面上,因此代码不需要运行所选的选项值,而是选择是否选择第一个选项。见下文澄清:

<select name="id[1]" id="attrib-1">//id cannot be used as it will be diff from page to     page
  <option value="20" selected="selected">Please Select</option>//can't be selected     when put into cart
  <option value="212" >Green</option>
  <option value="208">Yellow</option>
  <option value="210">Orange</option>
</select>

<input type="image" src="img/add.png" />//Add to cart button, has no id either.

这必须通过点击“添加到购物车”按钮来触发。所以我的伪代码是:

$('input[type=image]').click(function(){
if(select option: is first option select) {
run some code} else (if anyother option other the first) {
run some diff code
}

感谢您的帮助。我花了很多时间在这上面,... uggg!

1 个答案:

答案 0 :(得分:0)

没关系,我明白了

$('input[type=image]').click(function(){
    if ($("select > option:first").is(":selected")) {
        //run some code
} else  {
        //run some diff code
}
})