简体   繁体   English

获取表单中选择框的数组值

[英]Get the array value of select box within the form

In the case of having many products, and user can dynamically adding new dropdown box. 在拥有许多产品的情况下,用户可以动态添加新的下拉框。 When I click submit, I want to get the value of "category[]" within the form only. 当我单击提交时,我只想在表单中获取“ category []”的值。

动态下拉框预览

and this is the code: 这是代码:

<form name="store_category" method="post" enctype="multipart/form-data" action="{{ route('admin.product.category.store') }}">
    {!!csrf_field()!!}
    {!!""; $lang = session('locale') == "" ? "zh" : session('locale'); !!}
    <div class="form-group category-select-container">
        {!! Form::select("categories_$project->id[]", ${"categories_" . $lang}, null, ["class" => "input-sm"]) !!}
        <a href="#" class="btn btn-xs btn-danger btn-remove-category pull-right">{{trans('string.Remove')}}</a>
    </div>
    <div class="form-group">
        <button type="button" class="btn btn-dark btn-add-more-category">{{trans('string.Add More')}}</button>
        <button type="submit" class="btn blue btn-theme-colored">{{trans('string.submit')}}</button>
    </div>
</form>

I've tried to rename the array name to something else, but when the user dynamically adding new dropdown box, it couldn't follow the number of the project for the row. 我试图将数组名称重命名为其他名称,但是当用户动态添加新的下拉框时,它无法跟随该行的项目编号。

var template = '<div class="form-group category-select-container">'+
                    '{!! Form::select("categories_$project->id[]", ${"categories_" . $lang}, null, ["class" => "input-sm"]) !!}'+
                    '<a href="#" class="btn btn-xs btn-danger btn-remove-category pull-right">{{trans("string.Remove")}}</a>'+
                '</div>';
$('.btn-add-more-category').on('click', function(e){
    e.preventDefault();
    $(this).before(template);
})

I really would like to know what could be the output of this line in your Form::select() s 我真的很想知道Form::select()中此行的输出是什么

categories_$project->id[]

So anyway, if you would like to group your select tags you have to assign each of them with the same name. 因此,无论如何,如果要对选择标签进行分组,则必须为每个选择标签分配相同的名称。 Suppose you are looping in an array of $projects . 假设您循环执行$projects数组。 Each row would have 每行都有

<form name="store_category" method="post" enctype="multipart/form-data" action="{{ route('admin.product.category.store') }}"> 
    {!!csrf_field()!!} 
    @php $lang = session('locale') == "" ? "zh" : session('locale'); @endphp
    <div class="form-group category-select-container"> 
        {!! Form::select("category[]", ${"categories_" . $lang}, null, ["class" => "input-sm"]) !!} 
        <a href="#" class="btn btn-xs btn-danger btn-remove-category pull-right">{{trans('string.Remove')}}</a> 
    </div>
    <div class="form-group"> 
        <button type="button" class="btn btn-dark btn-add-more-category">{{trans('string.Add More')}}</button> 
        <button type="submit" class="btn blue btn-theme-colored">{{trans('string.submit')}}</button> 
    </div> 
</form>

Then next, you have to bind a click event to all your .btn-add-more-category . 接下来,您必须将click事件绑定到所有.btn-add-more-category Let's say your parent table has a class of .table 假设您的父表有一个.table

<script>
    $(function () {
        $(".table").on("click", ".btn-add-more-category", function(e) {
            var template = '<div class="form-group category-select-container"> ' +
                '{!! Form::select("category[]", ${"categories_" . $lang}, null, ["class" => "input-sm"]) !!} ' +
                '<a href="#" class="btn btn-xs btn-danger btn-remove-category pull-right">{{trans('string.Remove')}}</a>' +
            '</div>';
            var container = $(this).parent();
            $(template).insertBefore(container);
        });
    });
</script>

Also, I suggest that you move this line to your controller 另外,我建议您将此行移至控制器

$lang = session('locale') == "" ? "zh" : session('locale');

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

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