如何隐藏条件小于(<)的选择选项

时间:2016-08-09 18:18:12

标签: javascript jquery

有两个选择列表称为#ageSelect1#ageSelect2。我想在页面加载小于某个值时隐藏#ageSelect2选项。以下代码无效。但是

$("#ageSelect2 option[value =" + ageSelectStart + "]").hide();

代码正在运行。如何使用不到条件。?



<script type = "text/javascript" >
  $(document).ready(function() {
    var ageSelectStart = 21;
    var ageSelectEnd = 35;
    $("#ageSelect2 option[value <" + ageSelectStart + "]").hide();
  });
< /script>
&#13;
&#13;
&#13;

3 个答案:

答案 0 :(得分:0)

试试这个

&#13;
&#13;
$(window).load(function() {
  var ageSelectStart = 21;
  var ageSelectEnd = 35;
  var elSelect1 = $("#ageSelect1"),
      elSelect2 = $("#ageSelect2");
  
 elSelect1.find('option').each(function(index, el){
   if($(el).val() < ageSelectStart){
     //$(el).prop('disabled', true).hide();
     $(el).remove();
     }
 });

 elSelect2.find('option').each(function(index, el){
   if($(el).val() > ageSelectEnd){
     //$(el).prop('disabled', true).hide();
     $(el).remove();
     }
 });
});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<select name="startage" id="ageSelect1">
  <option value="10">10</option>
  <option value="11">11</option>
  <option value="21">21</option>
  <option value="22">22</option>
</select>
<select name="endage" id="ageSelect2">
  <option value="36">36</option>
  <option value="37">37</option>
  <option value="24">24</option>
  <option value="25">25</option>
</select>
&#13;
&#13;
&#13;

答案 1 :(得分:-1)

试试吧

<script type = "text/javascript" >
  $(document).ready(function() {
     var ageSelectStart = 21;
     var ageSelectEnd = 35;

     var selectedElement =document.getElementById("ageSelect2")
     for (var i=0; i<selectedElement.length; i++)
     {
        if (selectedElement.options[i].value < ageSelectStart)
        {
           selectedElement.options[i].hide();
        }
     }
  });
< /script>

答案 2 :(得分:-1)

&#13;
&#13;
<script type = "text/javascript" >
  $(document).ready(function() {
    var ageSelectStart = 21;
    var ageSelectEnd = 35;

    $("#ageSelect2").find("option").show().each(function() {
      if ($(this).attr("value") < ageSelectStart) {
        $(this).hide();
      }
    });
  }); 
</script>
&#13;
&#13;
&#13;