如何检查选择选项是否具有属性"值"?

时间:2015-12-31 18:25:25

标签: jquery

我有select这样:

<select id="select-filter-item" data-hasqtip="3" oldtitle="Seleziona un tipo di servizio e filtra gli appuntamenti visualizzati sul calendario." title="" aria-describedby="qtip-3">
    <optgroup label="Servizi" type="services-group">
        <option type="provider">Tutti i trattamenti</option>
        <option value="5CFCA63F-5550-4F18-9C03-FC5B50FC67CC" type="service">Taglio capelli </option>
        <option value="B626AD6A-E59B-4D7A-875B-CE3389B11E69" type="service">Colore capelli</option>
        <option value="93003E75-A757-43F0-B2E8-C5ACDD57DBB3" type="service">Trattamenti viso</option>
        <option value="3E9924C3-6AEB-4450-B534-C06DC30F9E35" type="service">Massaggi corpo</option>
    </optgroup>
</select>

我需要确定所选选项是否具有属性value。例如,第一个选项&#34; Tutti i trattamenti &#34;没有任何值属性。我怎样才能在jQuery中实现它?

5 个答案:

答案 0 :(得分:5)

一种解决方案是在所选选项上使用has attribute selector

$("#select-filter-item").find(":selected").is("[value]")

<强> Example Fiddle

答案 1 :(得分:0)

这应该有效:

 $("#select-filter-item").change(function() {
  var txt = $("#select-filter-item").find(":selected").is("[value]")

   if (txt) {
    alert("has a value")
  } else {
   alert("does not have a value")
  }
});

小提琴https://jsfiddle.net/7sm9xh31/3/

答案 2 :(得分:0)

希望这会有所帮助

$("#select-filter-item").on("change",function(){
    if ($('#select-filter-item option:selected').attr('value') != undefined) {
        console.log("the selected option have the attribute value.")
    }else {
        console.log("the selected option doesn't have any value.")
    }
});

答案 3 :(得分:0)

最简单的方法是使用纯javascript .hasAttribute()原生函数

JS Fiddle 1 - 检查控制台

var opts = $('#select-filter-item option');

opts.each(function(i) {
  if (!opts[i].hasAttribute("value")) {
    console.log('option-' + i + ': "' + opts[i].innerHTML + '" has no "value" attribute');
  }
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<select id="select-filter-item" data-hasqtip="3" oldtitle="Seleziona un tipo di servizio e filtra gli appuntamenti visualizzati sul calendario." title="" aria-describedby="qtip-3">
  <optgroup label="Servizi" type="services-group">
    <option type="provider">Tutti i trattamenti</option>
    <option value="5CFCA63F-5550-4F18-9C03-FC5B50FC67CC" type="service">Taglio capelli</option>
    <option value="B626AD6A-E59B-4D7A-875B-CE3389B11E69" type="service">Colore capelli</option>
    <option value="93003E75-A757-43F0-B2E8-C5ACDD57DBB3" type="service">Trattamenti viso</option>
    <option value="3E9924C3-6AEB-4450-B534-C06DC30F9E35" type="service">Massaggi corpo</option>
  </optgroup>
</select>

与上述相同,但使用vanilla javascript JS Fiddle 2

var opts = document.querySelectorAll('#select-filter-item option');

for (var i = 0; i < opts.length; i++) {
  if (!opts[i].hasAttribute("value")) {
    console.log('option-' + i + ': "' + opts[i].innerHTML + '" has no "value" attribute');
  }
}

<强>资源:

答案 4 :(得分:0)

<强> WORKING FIDDLE

你也可以这样做

if($("#select-filter-item").find(":selected").attr("value")!=null){
    alert("VALUE IS THERE");
}
else
{
    alert("VALUE IS NOT THERE");
}