Javascript检查多个下拉列表,条件返回false不起作用

时间:2015-11-14 13:29:56

标签: javascript jquery

我创建了多个下拉列表,并检查是否选择了任何类下拉列表。然后,它应该警告并且它正在这样做,但问题是if条件中的另一个函数也触发但是返回错误条件

只有在选择了所有类下拉列表时才会触发if条件

JS Fiddle demo

function addcourse() {

  // The .each() method is unnecessary here:
  $(".DD_Sub").each(function() {

    if ($("option:selected", this).val() == "  Select Subject  "); {

      alert("");
      return false;
    }
    
  });

  if ($("#BodyHolder_DDClass option:selected").text() == "  Select Class  ") {

    alert("Select class");

  }
  
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select name="ctl00$BodyHolder$DD_Sub1" class="DD_Sub" style="height:27px;width:150px;">
  <option value="Select Subject">Select Subject</option>
  <option value="Holiday">Holiday</option>
  <option value="Lunch">Lunch</option>
  <option value="ECO-20">ECO-20</option>
  <option value="ECO-21">ECO-21</option>
  <option value="ECO-22">ECO-22</option>
  <option value="ENG-20">ENG-20</option>

</select>
<select name="ctl00$BodyHolder$DD_Sub1" class="DD_Sub" style="height:27px;width:150px;">
  <option value="Select Subject">Select Subject</option>
  <option value="Holiday">Holiday</option>
  <option value="Lunch">Lunch</option>
  <option value="ECO-20">ECO-20</option>
  <option value="ECO-21">ECO-21</option>
  <option value="ECO-22">ECO-22</option>
  <option value="ENG-20">ENG-20</option>
</select>
<select name="ctl00$BodyHolder$DD_Sub1" class="DD_Sub" style="height:27px;width:150px;">
  <option value="Select Subject">Select Subject</option>
  <option value="Holiday">Holiday</option>
  <option value="Lunch">Lunch</option>
  <option value="ECO-20">ECO-20</option>
  <option value="ECO-21">ECO-21</option>
  <option value="ECO-22">ECO-22</option>
  <option value="ENG-20">ENG-20</option>
</select>
<select name="ctl00$BodyHolder$DD_Sub1" class="DD_Sub" style="height:27px;width:150px;">
  <option value="Select Subject">Select Subject</option>
  <option value="Holiday">Holiday</option>
  <option value="Lunch">Lunch</option>
  <option value="ECO-20">ECO-20</option>
</select>

<select name="ctl00$BodyHolder$DDClass" onchange="javascript:setTimeout('__doPostBack(\'ctl00$BodyHolder$DDClass\',\'\')', 0)" id="BodyHolder_DDClass" style="height:27px;width:150px;float:left;">
  <option value="Select Class">Select Class</option>
  <option value="I BA HEG">I BA HEG</option>
  <option value="I BA HEK">I BA HEK</option>
  <option value="I BA HEP">I BA HEP</option>
</select>

<input type="submit" name="ctl00$BodyHolder$Add_New_period_button" value="Add Period" onclick="return addcourse();" id="BodyHolder_Add_New_period_button">

1 个答案:

答案 0 :(得分:0)

尝试:

if($("option:selected").hasClass( "Select" )){
    alert("Not valid");
    return false;
}