-6ren">
gpt4 book ai didi

javascript - 将 javascript 循环值传递给 Laravel Controller

转载 作者:行者123 更新时间:2023-12-02 23:35:18 26 4
gpt4 key购买 nike

我想从多个选择中获取值并将它们传递给 Laravel Controller 。

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

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

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

<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[] 中?

最佳答案

使用带有复选框的多选

在 Blade 文件中:

<link rel="stylesheet" href="https://unpkg.com/multiple-select@1.3.1/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/multiple-select@1.3.1/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>

在 Controller 中:

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"
]
]

关于javascript - 将 javascript 循环值传递给 Laravel Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56311584/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com