我正在使用jquery根据篮球系列赛的获胜者动态淡出内容。所以,举个例子,让我们参加NBA东部决赛。这是七个系列中的佼佼者。第一支赢得四场比赛的球队获胜。因此,如果对于第一个输入标签,用户选择芝加哥公牛队作为他们的赢家。对于第二场比赛,用户选择迈阿密热火作为他们的赢家。根据这些结果到目前为止,jquery将不得不淡出一个额外的输入标签,因为很明显这个系列将至少进行5场比赛。
这是非常基本的。但是,如果客户要回去编辑游戏的获胜者,那么任何人都会有一些思考如何去做这件事,这会让他们相应地淡入淡出。我可以使用大量条件语句来做到这一点,但我真的很想得到一些思考,因为它与更有效的方法有关。
<script type="text/javascript">
$(document).ready(function(){
var data = <?php echo $teamone_ac; ?>;
var soulja = <?php echo $teamtwo_ac; ?>;
var away_team_other = $('.away_team_other').html();
var home_team_other = $('.home_team_other').html();
var away_team_wild = $('.away_team_wild').html();
var home_team_wild = $('.home_team_wild').html();
$("#game_one_other").autocomplete({ source: data });
$("#game_two_other").autocomplete({
source: data,
select: function(event, ui)
{
var game_one_other = $('#game_one_other').val();
var game_two_other = ui.item.value;
var game_three_other = $('#game_two_other').val();
var arr = [game_one_other, game_two_other, game_three_other, game_four_other, game_five_other];
var away = away_team_other;
var home = away_home_other;
var numAway = $.grep(arr, function (elem) {
return elem === away;
}).length;
var numHome = $.grep(arr, function (elem) {
return elem === home;
}).length;
if(game_one_other != game_two_other)
{
$('#one_four').fadeIn('slow');
}
}
});
$("#game_three_other").autocomplete({
source: data,
select: function(event, ui)
{
var game_one_other = $('#game_one_other').val();
var game_two_other = $('#game_two_other').val();
var game_three_other = ui.item.value;
if(game_two_other == game_three_other && game_two_other != game_one_other)
{
$('#one_four').fadeIn('slow');
}
}
});
$("#game_one_wild").autocomplete({ source: soulja });
$("#game_two_wild").autocomplete({ source: soulja });
$("#game_three_wild").autocomplete({ source: soulja });
$('#one_four').hide();
$('#one_five').hide();
$('#two_four').hide();
$('#two_five').hide();
});
答案 0 :(得分:0)
你有一张表格。该表格至少有n个元素(在这种情况下,n = 4)。每个元素可能是一个选择/无线电,用户可以从任何一个团队中选择。
<form id="predictWinners" action="submitPredict" method="POST">
<fieldset id="first">
<legend>Game 1</legend>
<input class="prediction" type="radio" name="winner" value="[team 1 here]" /> [team 1 here]
<br />
<input class="prediction" type="radio" name="winner" value="[team 2 here]" checked="checked" /> [team 2 here]
<br />
</fieldset>
<!-- repeat for first 4 -->
<input type="submit" value="Submit" />
</form>
将事件处理程序添加到其onchange方法
jQuery(document).ready(function(){
jQuery(".prediction").change(function() {
gamesNec = findGamesNecessary();
if(gamesNec > 4)
{
jQuery("fieldset").each(function(index){
if(index > 4 && index <=gamesNec) { jQuery(this).fadeIn() }
});
}
});
})
编写一个事件处理程序,根据用户选择计算每个团队的胜利,并将其与最大值进行比较。
Go through each fieldset并根据剩余的数量将其淡化/创建
你可以编写一个bestOf(x)方法来计算出需要多少次胜利(对于NBA季后赛来说,这是7次,但计算这将使你的代码具有可移植性和可扩展性。
bestOf = function(x) {
return (x % 2 == 0) ? (x / 2) : ((x/2)+0.5)
}
findGamesNecessary = function() {
seriesLength = 7
team1_wins = 0
team2_wins = 0
//iterate through each user selection and update the wins. Left as an exercise
gamesPlayed = team1_wins + team2_wins
gamesNecessary = (team1_wins >1 && team2_wins > 1) ? bestOf(seriesLength-gamesPlayed) + gamesPlayed : bestOf(seriesLength)
return gamesNecessary;
}