如何使用jQuery过滤选项?

时间:2017-12-14 12:19:48

标签: javascript jquery html

如何过滤选项...

当我选择城市电影和剧院下拉后,我选择电影或剧院改变电影播放剧院或剧院播放电影...............

如果我选择了一个选项来显示选项相关信息

下面是我的HTML代码



$(document).ready(function() {
  var cityData = [{
      cityName: 'Bengaluru',
      value: "Bengaluru",
      data: [{
          movieName: 'ABC',
          theaterName: 'Tulsi Theatre'
        },
        {
          movieName: 'DEF',
          theaterName: 'PVR'
        },
        {
          movieName: 'GHI',
          theaterName: 'Srinivasa Theatre'
        }
      ]
    },


    {
      cityName: 'Hyderabad',
      value: "Hyderabad",
      data: [{
          movieName: '123',
          theaterName: 'Theatre1'
        },
        {
          movieName: '456',
          theaterName: 'PVR2'
        },
        {
          movieName: '789',
          theaterName: 'Theatre3'
        }
      ]
    },

    {
      cityName: 'Guntur',
      value: "Guntur",
      data: [{
          movieName: 'ABC1',
          theaterName: 'Theatre4'
        },
        {
          movieName: 'DEF2',
          theaterName: 'PVR3'
        },
        {
          movieName: 'GHI3',
          theaterName: 'Theatre5'
        }
      ]
    },

    {
      cityName: 'Ongole',
      value: "Ongole",
      data: 'currently not available'
    }
  ];

  $("#selectCity").on('change', function() {
    var locations = cityData.filter(c => c.cityName === $(this).val())[0].data;
    var locationString = '';
    var locationString2 = '';
    console.log(locations)
    $.each(locations, function(i, item) {

      console.log(JSON.stringify(item));
      locationString += '<option value="' + item.theaterName + '">' + item.theaterName + '</option>';
      locationString2 += '<option value="' + item.movieName + '">' + item.movieName + '</option>';
    });
    $('#secondselectbox').html(locationString);
    $('#thirdselectbox').html(locationString2);
  });
});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="UserData">
  <h1>MyMovie-Ticket-Booking</h1>
  <select class="selectCity" id="selectCity">
    <option value="City">Select City</option>
    <option value="Bengaluru">Bengaluru</option>
    <option value="Hyderabad">Hyderabad</option>
    <option value="Guntur">Guntur</option>
    <option value="Ongole">Ongole</option>
  </select>
  <span id="welcome"> </span>
</div>
<div>
  <select id="secondselectbox"></select>
  <select id="thirdselectbox"></select>
</div>
&#13;
&#13;
&#13;

如何更改我在第一个下拉菜单中选择电影名称的时间,以便在另一个下拉列表中更改相关的电影院名称...

更改电影名称并显示正在播放电影的影院.....

2 个答案:

答案 0 :(得分:1)

&#13;
&#13;
$(document).ready(function() {
  var cityData = [{
      cityName: 'Bengaluru',
      value: "Bengaluru",
      data: [{
          movieName: 'ABC',
          theaterName: 'Tulsi Theatre'
        },
        {
          movieName: 'DEF',
          theaterName: 'PVR'
        },
        {
          movieName: 'GHI',
          theaterName: 'Srinivasa Theatre'
        }
      ]
    },


    {
      cityName: 'Hyderabad',
      value: "Hyderabad",
      data: [{
          movieName: '123',
          theaterName: 'Theatre1'
        },
        {
          movieName: '456',
          theaterName: 'PVR2'
        },
        {
          movieName: '789',
          theaterName: 'Theatre3'
        }
      ]
    },

    {
      cityName: 'Guntur',
      value: "Guntur",
      data: [{
          movieName: 'ABC1',
          theaterName: 'Theatre4'
        },
        {
          movieName: 'ABC1',
          theaterName: 'PVR3'
        },
        {
          movieName: 'GHI3',
          theaterName: 'Theatre5'
        }
      ]
    },

    {
      cityName: 'Ongole',
      value: "Ongole",
      data: 'currently not available'
    }
  ];
  $("#selectCity").on('change', function() {
    var locations = cityData.filter(c => c.cityName === $(this).val())[0].data;
    var locationString2 = '';
    console.log(locations)
    $.each(locations, function(i, item) {
        locationString2 += '<option value="' + item.movieName + '">' + item.movieName + '</option>';
    });
    $('#thirdselectbox').html(locationString2);
    createTheaterDropdown();
});
   $("#thirdselectbox").on('change', function() {
     createTheaterDropdown();

   });
   function createTheaterDropdown(){
     var locations = cityData.filter(c => c.cityName === $('#selectCity').val())[0].data;
      var movie = locations.filter(c => c.movieName === $('#thirdselectbox').val());
      var locationString = '';
      $.each(movie, function(i, item) {
        locationString += '<option value="' + item.theaterName + '">' + item.theaterName + '</option>';
      })

    $('#secondselectbox').html(locationString);
   }
})
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<h1>MyMovie-Ticket-Booking</h1>
  <select class="selectCity" id="selectCity">
                    <option value="City">Select City</option>
                    <option value="Bengaluru">Bengaluru</option>
                    <option value="Hyderabad">Hyderabad</option>
                    <option value="Guntur">Guntur</option>
                    <option value="Ongole">Ongole</option>
                </select>
  <span id="welcome"> </span>
</div>
<div>
  <select id="thirdselectbox"></select>
  <select id="secondselectbox"></select>
</div>
&#13;
&#13;
&#13;

您可以在选择城市名称之后过滤影院名称,此处是

的代码
$("#selectCity").on('change', function () {
    var locations = cityData.filter(c => c.cityName === $(this).val())[0].data;
    var locationString2 = '';
    console.log(locations)
    $.each(locations, function (i, item) {
        locationString2 += '<option value="' + item.movieName + '">' + item.movieName + '</option>';
    });
    $('#thirdselectbox').html(locationString2);
    createTheaterDropdown();
});
$("#thirdselectbox").on('change', function () {
    createTheaterDropdown();

});

function createTheaterDropdown() {
    var locations = cityData.filter(c => c.cityName === $('#selectCity').val())[0].data;
    var movie = locations.filter(c => c.movieName === $('#thirdselectbox').val());
    var locationString = '';
    $.each(movie, function (i, item) {
        locationString += '<option value="' + item.theaterName + '">' + item.theaterName + '</option>';
    })

    $('#secondselectbox').html(locationString);
}

答案 1 :(得分:0)

或者,您可以通过添加自定义属性来确保每部电影与影院相关联,即:data-theater=item.theaterName

然后您可以使用影院下拉列表的更改事件隐藏/显示相关电影,而不是不断重建DOM。

$(document).ready(function() {
  var cityData = [{
      cityName: 'Bengaluru',
      value: "Bengaluru",
      data: [{
          movieName: 'ABC',
          theaterName: 'Tulsi Theatre'
        },
        {
          movieName: 'DEF',
          theaterName: 'PVR'
        },
        {
          movieName: 'GHI',
          theaterName: 'Srinivasa Theatre'
        }
      ]
    },


    {
      cityName: 'Hyderabad',
      value: "Hyderabad",
      data: [{
          movieName: '123',
          theaterName: 'Theatre1'
        },
        {
          movieName: '456',
          theaterName: 'PVR2'
        },
        {
          movieName: '789',
          theaterName: 'Theatre3'
        }
      ]
    },

    {
      cityName: 'Guntur',
      value: "Guntur",
      data: [{
          movieName: 'ABC1',
          theaterName: 'Theatre4'
        },
        {
          movieName: 'DEF2',
          theaterName: 'PVR3'
        },
        {
          movieName: 'GHI3',
          theaterName: 'Theatre5'
        }
      ]
    },

    {
      cityName: 'Ongole',
      value: "Ongole",
      data: 'currently not available'
    }
  ];

  $("#selectCity").on('change', function() {
    var locations = cityData.filter(c => c.cityName === $(this).val())[0].data;
    var locationString = '';
    var locationString2 = '';
    console.log(locations)
    $.each(locations, function(i, item) {

      console.log(JSON.stringify(item));
      locationString += '<option value="' + item.theaterName + '">' + item.theaterName + '</option>';
      locationString2 += '<option data-theater="' + item.theaterName + '" value="' + item.movieName + '">' + item.movieName + '</option>';
    });
    $('#secondselectbox').append(locationString);
    $('#thirdselectbox').append(locationString2);
    
    // ensure the movies are initialized correctly based on the current theater
    theaterChanged.call($('#secondselectbox'));
  });

  $("#secondselectbox").on('change', theaterChanged);

  function theaterChanged() {
    var theater = this.value;
    $('#thirdselectbox option').attr('hidden', true);
    $('#thirdselectbox option[data-theater="' + theater + '"]').attr('hidden', false);
    $("#thirdselectbox option[hidden!=hidden]").eq(0).prop("selected", true);
  }
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="UserData">
  <h1>MyMovie-Ticket-Booking</h1>
  <select class="selectCity" id="selectCity">
        				<option value="City">Select City</option>
        				<option value="Bengaluru">Bengaluru</option>
        				<option value="Hyderabad">Hyderabad</option>
        				<option value="Guntur">Guntur</option>
        				<option value="Ongole">Ongole</option>
        			</select>
  <span id="welcome"> </span>
</div>
<div>
  <select id="secondselectbox"></select>
  <select id="thirdselectbox"></select>
</div>