如何检查select中是否存在某个属性

时间:2015-10-31 16:32:59

标签: jquery html

我有这个html选择:

<select id="select-filter-item" data-hasqtip="true" title="" aria-describedby="qtip-3">
        <optgroup label="Servizi" type="services-group">
              <option type="provider">Tutti i trattamenti</option>
              <option value="13" type="service">Taglio</option>
              <option value="14" type="service">Colore</option>
        </optgroup>
</select>

如何看到我没有value属性的第一个选项,我想检查从select中选择的当前值是否具有属性value,如果是,则获取{{1数字为13或14.我怎么能做到这一点?

我得到的值如下:

value

2 个答案:

答案 0 :(得分:2)

您可以使用isNaN()查看该值是否为数字

$('#select-filter-item').change(function(){
    var val = $(this).val();
    var isNumber = val !='' && !isNaN(val );
    alert(isNumber);
});

答案 1 :(得分:1)

var selected = $("#select-filter-item option:selected"), 
    val = $(selected).val();

if(val){ //this checks if the value is not falsy (0, undefined, null, "", false)
   if(typeof val === "string"){
      //do something with your string
   } else if(typeof val === "number") {
      //do something with your number
   }
}


/* You can also use jQuery instead of css selector 
   to go through a div tree, as far as I know it has better
   performance 
*/

//var selected = $("#select-filter-item").find("option:selected");