我想从多个选择中获取值,并将其传递给Laravel控制器。

从多个选择中获取所有值:

<script>
    function redirect() {
        $("#sbtwo > option").each(function() {
            if(document.getElementById("report1").checked == true)
                window.open (+this.value+"/report1", 'w1');
        });
    }
</script>


我想获取+this.value+并将其传递给Laravel控制器。

<form action="/teachers/printreports" method="POST">
    {{ csrf_field() }}
    <input type="checkbox" name="report1[]"  id="report1"> Basic Data</div>
    <input type="submit" class="btn btn-primary " value="Print" >
</form>


如何将javascript +this.value+存储为表单元素name= report1[]

最佳答案

使用多选复选框

在刀片文件中:

<link rel="stylesheet" href="https://unpkg.com/[email protected]/dist/multiple-select.min.css">
<script type="text/javascript" src="https://code.jquery.com/jquery-3.4.1.js"></script>
<script src="https://unpkg.com/[email protected]/dist/multiple-select.min.js"></script>


<form action="/teachers/printreports" method="POST">
    @csrf

    <select name="reports[]" id="select" multiple="multiple">
        <option value="1">Report-1</option>
        <option value="2">Report-2</option>
        <option value="3">Report-3</option>
        <option value="4">Report-4</option>
        <option value="5">Report-5</option>
        <option value="6">Report-6</option>
        <option value="7">Report-7</option>
        <option value="8">Report-8</option>
    </select>

    <input type="submit" class="btn btn-primary " value="Print" >
</form>

<script>
    $(document).ready(function(){
        $(function () {
            $('.select').multipleSelect({
                width: 500
            })
        })
    });
</script>


在控制器中:

public function yourFunction(Request $request){
    $post = $request->all();

    // dump your selected reports
    dd($post);
}


结果:

array:3 [▼
  "_token" => "WXs56rP5fWWw3D4B1H8I1wibT23Dko2AfFPmFRiZ"
  "reports" => array:5 [▼
    0 => "1"
    1 => "2"
    2 => "4"
    3 => "6"
    4 => "7"
  ]
]

10-07 12:39