如何产生除数的值

时间:2019-04-30 18:21:08

标签: javascript jquery arrays

我有23支球队,我想将他们分成1至4组。当我分成两组时,它可以工作,但是当我分成3-4组时,总共不是23个团队,是否可以共享它们来输入文本?

我在想逻辑是否是这样

团队总数:23个团队 当我分成一组 A组:23支球队

如果我分成两组 A组:12支 B组:11支球队

如果我分为3组 A组:8支 B组:8支 C组:7支球队

所以球队总数还是一样

form image

    $('#total_group').on('change', function() {
      var total_group = $('#total_group').val();
      var participant = $('#team_ready').val();
     if (total_group != "") {
        // var first = Math.ceil(participant/total_group);
        var divided = Math.ceil(participant / total_group);
        var first = divided;
        var second = first - participant;
        var third = first - second;
        var fourth = second - third;
        var answer = [first, second, third, fourth];
        $('#split_group').css("display", "flex");
        var grouping = [];
        var alpha = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'];
        for (var i = 0; i < total_group; i++) {
          var content_data = "<div class='form-group' align='center' style='margin-bottom:5px;'><label>GROUP " + alpha[i] + "</label><input type='text' id='url_partner' name='url_partner' class='form-control' value='" + answer[i] + "'  required></div>";
          grouping.push(content_data);
        }
        var split_group = grouping.join('');
        $('#split_group').html(split_group);
      } else {
        $('#split_group').css("display", "none");
      }
    });  

my code image

请帮助

2 个答案:

答案 0 :(得分:2)

由于23不能很好地分解为2、3或4,因此您可以创建一个常规功能,例如将任意一组团队分成若干组

class Category(models.Model):
    name = models.CharField(max_length=250, db_index=True, unique=True)
    slug = models.SlugField(max_length=250, db_index=True, unique=True)

class SubCategory(models.Model):
    parent = models.ForeignKey(Category, on_delete=models.CASCADE)
    name = models.CharField(max_length=250, db_index=True)
    slug = models.SlugField(max_length=250, db_index=True)

class Product(models.Model):  
    parent = models.ForeignKey(Category, on_delete=models.CASCADE, related_name="category")
    child = models.ForeignKey(SubCategory, on_delete=models.CASCADE)
    name = models.CharField(max_length=250, unique=True, db_index=True)
    slug = models.SlugField(max_length=250, unique=True, db_index=True)

然后您可以在任何地方调用此功能,以将任意数量的团队分为任意数量的组(如您所指出的那样,并非总是均匀的)

例如:

@admin.register(Category)
class CategoryAdmin(admin.ModelAdmin):
    list_display = [
        'name', 'slug'
    ]

@admin.register(SubCategory)
class SubCategoryAdmin(admin.ModelAdmin):
    prepopulated_fields = {'slug':('name',)}
    list_display = [
        'name', 'slug', 'parent',
    ]
@admin.register(Product)
class ProductAdmin(admin.ModelAdmin):
    list_display = [
        'name', 'slug', 'child', 
    ]
    prepopulated_fields = {'slug':('name',)}
    list_per_page = 20

    class Media:
        js = ('javascript/hide_child_if_parent_is_none.js', )

答案 1 :(得分:0)

$('#total_group').on('change',function(){
        var total_group = $('#total_group').val();
        // var participant = $('#team_ready').val();

        if(total_group!=""){
            // var first = Math.ceil(participant/total_group);
            var divided = Math.ceil(23/total_group);
            var participant = 23;
            if(total_group=="1"){
                var first=divided;
                var second=participant-first;
                var third="";
                var fourth="";
            }else if(total_group=="2"){
                var first=divided;
                var second=participant-first;
                var third="";
                var fourth="";
            }else if(total_group=="3"){
                var first=divided;
                var second=divided;
                var third=participant-first-second;
                var fourth="";
            }else if(total_group=="4"){
                var first=divided;
                var second=divided;
                var third=divided;
                var fourth=participant-first-second-third;
            }
            var answer = [first,second,third,fourth];

            $('#split_group').css("display","flex");
            var grouping=[];
            var alpha = ['A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'];
            for(var i=0;i < total_group;i++){
                var content_data="<div class='form-group' align='center' style='margin-bottom:5px;'><label>GROUP "+alpha[i]+"</label><input type='text' id='url_partner' name='url_partner' class='form-control' value='"+answer[i]+"'  required></div>";
                grouping.push(content_data);
            }
            var split_group=grouping.join('');
            $('#split_group').html(split_group);
        }else{
            $('#split_group').css("display","none");
        }
    });