在提交之前检查是否检查了所有动态单选按钮

时间:2016-12-22 16:12:13

标签: javascript html laravel validation laravel-5

我不太确定在提交表单之前如何检查来自数据库的所有动态单选按钮是否都可以检查。 我正在进行预订项目,线索是关于食品包装和分包装。根据客户选择的包裹,他会在提交和进入预订视图之前像一个单选按钮一样检查他想要的菜。 如果不是所有产品都被选中(每个类别1个),则表格无法提交。我的观点:

public IList<IDictionary<string, string>> BuildDictionary<T>(HashSet<T> sourceSet, Func<T, string> toKey, Func<T, string> toMemberValue)
{
  IList<IDictionary<string, string>> data = new List<IDictionary<string, string>>();

  foreach (var element in sourceSet)
  {
    Dictionary<string, string> newLookup = new Dictionary<string, string>();
    newLookup.Add(toKey(element), $"{toKey(element)},{toMemberValue(element)}");
    data.Add(newLookup);
  }

  return data;
}

void Main()
{
  HashSet<ClassA> setOfAs = new HashSet<ClassA>(new[] { new ClassA { Code = "foo", Name = "bar" }, new ClassA { Code = "foo2", Name = "bar2" } });
  HashSet<ClassB> setOfBs = new HashSet<ClassB>(new[] { new ClassB { Code = "foo", Name = "bar" }, new ClassB { Code = "foo2", Name = "bar2" } });

  var lookupOfAs = BuildDictionary(setOfAs, x => x.Code, x => x.Name);
  var lookupOfBs = BuildDictionary(setOfBs, x => x.Code, x => x.Name);
}

// Define other methods and classes here
public class ClassA
{
  public string Code { get; set; }
  public string Name { get; set; }
}

public class ClassB
{
  public string Code { get; set; }
  public string Name { get; set; }
}

和我的控制员:

 {!! Form::open(array('route' => ['continue_pacakage_booking',$sub_package->id],'id' => 'form','class'=>'form-horizontal','novalidate' => 'novalidate','role' => 'form', 'enctype' => 'multipart/form-data', 'onsubmit' => "validateForm()")) !!}
@foreach($dishes_and_categories as $key=>$dishes_and_category)
    <section style="padding: 70px 50px">
        <div class="container"style="border-bottom: 10px solid #F0F0F0" >
            <div class="row">
                <!-- section title -->
                <div class="col-md-12 text-center">
                    <span class="title-small text-uppercase letter-spacing-3 font-weight-600" style=" color: #da7e57;">{{$dishes_and_category['category_name']}}</span>
                    <div class="separator-line-thick bg-black no-margin-bottom margin-one xs-margin-top-five"></div>
                </div>
                <!-- end section title -->
            </div>
            <section class="wow fadeIn blog-details-text" style="padding-top: 30px;padding-bottom: 30px">
                <div class="container">
                    @foreach($dishes_and_category['dishes'] as $dish)
                        <div class="col-md-4 col-sm-6">
                            <label>
                                <input type="radio" @if($dishes_and_category['id_category'] == 6) disabled @endif name="{{$dishes_and_category['id_category']}}" value="{{$dish->id}}" />
                                <img src="http://develop.almotech.org/restaurant/restaurant_panel/public/attachment/dishes/{{$dish->image}}" style="border-radius: 50%;height: 250px;width: 100%">
                                <p style="text-align: center; font-weight: bold" class="text-uppercase">{{$dish->name}}</p>
                            </label>
                        </div>
                    @endforeach
                </div>
            </section>
        </div>
    </section>
@endforeach
<div class="col-md-12">
<button class="highlight-button btn btn-large btn-round button xs-margin-bottom-five" type="submit"  id="select_table" name="select_table" style="margin-left: 805px; width: 278px; margin-bottom: 20px">Continua Prenotare</button>

</div>
{!! Form::close() !!}

2 个答案:

答案 0 :(得分:0)

你可以结合使用jQuery函数(不是 - http://api.jquery.com/not/,has - http://api.jquery.com/has/)来帮助你。

if ($('#testForm:not(:has(:radio:checked))').length) {
    alert('Not all checked');
}

这是我创建的http://codepen.io/ltong/pen/dOLmoa示例。

答案 1 :(得分:0)

您需要更改按钮表单的名称。

name="{{$dishes_and_category['id_category']}}

name="buttons[{{$dishes_and_category['id_category']}}]"

然后你可以简单地在

上做一个foreach
In the controller request->input('buttons');

或javascript中的foreach

var radios = document.getElementsByName( buttons);
    for( i = 0; i < radios.length; i++ ) {
        if( radios[i].checked ) {
            do somthing
        }
    }