jQuery选择器用于具有空值属性的选择元素吗? [英] jQuery selector for select elements with empty value attributes?

查看:334
本文介绍了jQuery选择器用于具有空值属性的选择元素吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用下面的选择器来获取所有预先填充有值的表单输入.

I'm using the below selector to get all the form inputs that are pre-populated with a value.

$('input[value!=""]');

这很好用,所以我认为我可以使用以下选择器将相同的逻辑应用于选择元素,尽管它似乎是在Chrome中选择所有选择元素.

This works great and so I thought I could apply the same logic to my select elements using the following selector, though it seems to be selecting all the select elements in Chrome.

$('select[value!=""]');

下面是表单上两种选择的示例:

Below is an example of two types of selects on the form:

<select name="phone2_type" id="phone2_type">
  <option value="">Select one:</option>
  <option value="HOME">Home</option>
  <option value="CELL">Cell</option>
  <option value="BUSN">Business</option>
</select>

<select name="state" id="state">
  <option value="">Select one:</option>
  <option value="XX">Outside USA/Canada</option>
  <option value="AL" selected="selected">Alabama</option>
  <option value="AK">Alaska</option>
  ...
</select>

我想选择第二个选择,因为它具有已经使用select ="selected"

I'd like to select the second select since it has a value selected already with the select="selected"

推荐答案

$('select').has('option[selected="selected"]')

将获得选择元素. jsFiddle示例 .

will get the select element. jsFiddle example.

这篇关于jQuery选择器用于具有空值属性的选择元素吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆