我的 laravel
应用程序中有一堆复选框,我想将其保存为 MySQL
数据库中的数组,但我不知道该怎么做它。
这是我的输入字段:
<input type="checkbox" class="form-check-input" name="vehicle[]" value="bmw">BMW
<input type="checkbox" class="form-check-input" name="vehicle[]" value="audi">Audi
<input type="checkbox" class="form-check-input" name="vehicle[]" value="mercedes">Mercedes
<input type="checkbox" class="form-check-input" name="vehicle[]" value="chrysler">Chrysler
<input type="checkbox" class="form-check-input" name="vehicle[]" value="chevrolet">Chevrolet
<input type="checkbox" class="form-check-input" name="vehicle[]" value="ford">Ford
对于简单的输入字段,我在 .js
文件中执行此操作:
var formData = new FormData();
formData.append(
"name",
$("#uploadModal")
.find('input[name="name"]')
.val()
);
// etc. etc,
然后我使用 axios
提交数据:
axios.post($("#uploadModal form").attr("action"), formData) ...
如何将选中的 checkbox
数组添加到 formData 中?
最佳答案
<input type="checkbox" class="form-check-input" name="vehicle[]" value="bmw">BMW
<input type="checkbox" class="form-check-input" name="vehicle[]" value="audi">Audi
<input type="checkbox" class="form-check-input" name="vehicle[]" value="mercedes">Mercedes
<input type="checkbox" class="form-check-input" name="vehicle[]" value="chrysler">Chrysler
<input type="checkbox" class="form-check-input" name="vehicle[]" value="chevrolet">Chevrolet
<input type="checkbox" class="form-check-input" name="vehicle[]" value="ford">Ford
<script>
function sendCall(){
var formData = new FormData();
var vehicleList = [];
$("[name='vehicle[]']").each(function(){
if($(this).is(":checked")){
vehicleList.push($(this).val());
}
});
formData.append("vehicle",vehicleList);
---> add axios request with formData object
}
--> call sendCall on click btn
</script>
关于javascript - 如何使用Javascript将复选框数组保存在MySQL数据库中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55968426/