迭代具有相同类名的选择下拉列表以查找特定选项的选择

时间:2015-07-14 21:31:40

标签: javascript jquery html select

我想搜索具有相同类名的选择框,以发现是否有人选择了特定选项......

<select class='myclass' id='instance1'>
  <option value="volvo">Volvo</option>
  <option value="saab">Saab</option>
  <option value="mercedes">Mercedes</option>
  <option value="audi">Audi</option>
</select> 

 <select class='myclass' id='instance2'>
  <option value="volvo">Volvo</option>
  <option value="saab">Saab</option>
  <option value="mercedes">Mercedes</option>
  <option value="audi">Audi</option>
</select> 


 <select class='myclass' id='instance3'>
  <option value="volvo">Volvo</option>
  <option value="saab">Saab</option>
  <option value="mercedes">Mercedes</option>
  <option value="audi">Audi</option>
</select> 

<script>

$('.myclass').each(function(i, obj) {
        // if anyone wants an audi, let me know
        if (something == 'audi')
            alert("found!");
 });

3 个答案:

答案 0 :(得分:2)

这样:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>$archiver</key>
    <string>NSKeyedArchiver</string>
    <key>$objects</key>
    <array>
        <string>$null</string>
        <string>Robert</string>
        <string>Smith</string>
    </array>
    <key>$top</key>
    <dict>
        <key>double</key>
        <real>123.456</real>
        <key>first</key>
        <dict>
            <key>CF$UID</key>
            <integer>1</integer>
        </dict>
        <key>last</key>
        <dict>
            <key>CF$UID</key>
            <integer>2</integer>
        </dict>
        <key>pi</key>
        <real>3.1415927410125732</real>
    </dict>
    <key>$version</key>
    <integer>100000</integer>
</dict>
</plist>

您可能需要在函数中使用JS代码:)

答案 1 :(得分:2)

&#13;
&#13;
$('#check').click(function(){
$('.myclass').each(function(i, obj) {
        // if anyone wants an audi, let me know
 
     if (obj.value== 'audi'){
            alert("found!");
    }else{
        alert('Not found');
    }
 });});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button id="check">Check</button>
<select class='myclass' id='instance1'>
  <option value="volvo">Volvo</option>
  <option value="saab">Saab</option>
  <option value="mercedes">Mercedes</option>
  <option value="audi">Audi</option>
</select> 

 <select class='myclass' id='instance2'>
  <option value="volvo">Volvo</option>
  <option value="saab">Saab</option>
  <option value="mercedes">Mercedes</option>
  <option value="audi">Audi</option>
</select> 


 <select class='myclass' id='instance3'>
  <option value="volvo">Volvo</option>
  <option value="saab">Saab</option>
  <option value="mercedes">Mercedes</option>
  <option value="audi">Audi</option>
</select> 
&#13;
&#13;
&#13;

答案 2 :(得分:2)

您需要一个按钮来处理检测,或者您可以bindselect更改事件处理程序并处理新选择的选项。

要通过按钮点击实现它,请将以下内容添加到HTML中。

<button id="checkSelected">Check for Audi</button>

然后在你的JavaScript / jQuery代码中:

$("#checkSelected").on("click", function() {
    $("select[class='myclass'] option:selected").each(function() {
      var element = $(this);
      if (element.val() === "audi") {
         alert('The user has selected an Audi in combobox ' + element.parent().attr("id"));   
      }
    }); 
});

这将横向DOM并为类myclass选择组合框的所有选定元素,并检查该值是否等于audi并提醒用户从哪个框中选择它。

http://jsfiddle.net/0w0v06et/