简体   繁体   English

JavaScript / jQuery-从2个下拉菜单中获取输入

[英]JavaScript / jQuery - taking input from 2 drop down identical menus

I have 2 drop down lists, which both hold the same list of teams , one to be used as the home team and one as the away team. 我有2个下拉列表,这既持有的同一列表teams ,一个被用作home团队和一个作为away球队。 At the moment the first drop down list works, when a team is selected from the list, it's id and name is output to the page. 目前第一个下拉列表有效,当从列表中选择一个team时,其idname将输出到页面。 But when the other drop down is clicked, nothing happens. 但是,当单击另一个下拉菜单时,什么也不会发生。 So for example the output takes the id and team name and outputs them to the textboxes. 因此,例如,输出使用ID和小组名称并将其输出到文本框。

Here is an example of each drop down list and the relevant code below, can anyone help me out? 这是每个下拉列表的示例以及下面的相关代码,有人可以帮助我吗?

HTML generated for home team list: 为主队列表生成的HTML:

<select id="teamList" style="width: 160px;">
<option></option>
<option id="1362174068837" value="1362174068837" class="teamDropDown">Liverpool</option></select>

HTML generated for away team list: 为客队列表生成的HTML:

<select id="teamList" style="width: 160px;">
<option></option>
<option id="1362174068837" value="1362174068837" class="teamDropDown">Liverpool</option>
</select>

JADE template used to generate the HTML (used for both lists): 用于生成HTML的JADE模板(用于两个列表):

div#teamDropDownDiv
    -if(teamsList.length > 0){
        select#teamList(style='width: 160px;')
            option
                -each team in teamsList
                    option.teamDropDown(id="#{team.key}",value="#{team.key}") #{team.name}

JavaScript for the page: 页面的JavaScript:

 Team.initTeamsDD = function(){
  $("#teamList").change(function(e){
    e.preventDefault();
    var teamId = $(this).val();

    $.get('/show/team/'+teamId, function(response){
      if(response.retStatus === 'success'){
        var teamData = response.teamData;
        $('#teamId').val(teamData.key);
        $('#teamName').val(teamData.name);
      } else if(response.retStatus === 'failure'){

      }
    });  
  });

The two <select> elements both have the same "id" value, which is "teamList". 两个<select>元素都具有相同的“ id”值,即“ teamList”。 You should never have two elements with the same "id". 您永远不应有两个具有相同“ id”的元素。 Because of this, the on-change event handler is getting attached to only one of them. 因此,变更事件处理程序仅附加到其中一个。 You should change them to "homeTeamList" and "awayTeamList", and then use: 您应该将它们更改为“ homeTeamList”和“ awayTeamList”,然后使用:

Team.initTeamsDD = function(){
  $("#homeTeamList, #awayTeamList").change(function(e){
...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM