douba4275 2017-06-26 07:47
浏览 30
已采纳

如何从所选项目选项生成大多数出现值的递增列表?

I have a form that has two event options, each event has 3 different items each and each item has 2-3 participants.

Event 1

  1. Football – John, Mike, Sam
  2. Cricket – Sam, Henry
  3. Chess – Ross,Mike

Event 2

  1. Guitar – Sam, Phillip
  2. Drum – Steve, Ross
  3. Piano – Mike, John
<form>
    <div>
        <h1>Select Event 1</h1>
        <select>
            <option>Football</option>
            <option>Cricket</option>
            <option>Chess</option>
        </select>
    </div>
    <div>
        <h1>Select Event 2</h1>
        <select>
            <option>Guitar</option>
            <option>Drum</option>
            <option>Piano</option>
        </select>
    </div>
    <input type="submit" /> 
</form>

How do I generate a result that makes a list of people who are involved on those selected items and he who has maximum participation among those events appears first and then the rest. Like, when someone selects 'Football' from Event 1 and 'Guitar' from Event 2, after submission, the result will be –

Participants

  1. Sam
  2. Henry, Phillip

How do I set the values of the options and how php or javascript can help me generate the result?

  • 写回答

1条回答 默认 最新

  • doumi9661 2017-06-26 08:04
    关注

    You can set an array of object for map your participants with the events. Here is a logic who can be implemented. It works for one event selection. You just have to loop into each select.

    var participants = [{
      name:"Sam",
      events:["Football","Guitar"]
    },{
      name:"Philip",
      events:["Drum","Cricket","Guitar"]
    }];
    
    $(".event-selection").change(function(){
      var selectedEvent = $(this).find("option:selected").text();
      var results = participants.filter(function(p){
        return p.events.indexOf(selectedEvent) > -1;
      }).map(function(p){return p.name}).join(", ");
      $(".results").text(results);
    });
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <form>
        <div>
            <h1>Select Event 1</h1>
            <select class="event-selection" id="event1">
                <option>Football</option>
                <option>Cricket</option>
                <option>Chess</option>
            </select>
        </div>
        <div>
            <h1>Select Event 2</h1>
            <select class="event-selection" id="event2">
                <option>Guitar</option>
                <option>Drum</option>
                <option>Piano</option>
            </select>
        </div>
    </form>
    <div class="results">
    </div>

    </div>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 unity第一人称射击小游戏,有demo,在原脚本的基础上进行修改以达到要求
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line