我在將輸入“值”引數分配給 Inertiajs 中的 useForm 變數時遇到問題。
這是場景
1-我有一個包含 6 個欄位的表單,所有欄位都是文本型別。2-有一個隱藏的輸入欄位,其中包含用戶 ID。3- 我會將這些資料傳遞給控制器??進行進一步處理。 問題
由于我使用的是 Inertiajs,所以表單值是反應性的,但問題是如何在其中傳遞隱藏欄位?
表格代碼
<form @submit.prevent="submit"> <br> <br>
<!-- The hidden form field will store the value of child ID and save it in table -->
<div v-for="data in sData" :key="data.ID">
<input type="text" name="ChildID"
:value="data.ChildID">
</div>
<input type="text"
name="hobbie"
placeholder="Enter hobbies"
v-model="form.hobbie"
required
>
<br> <br>
<input type="text"
name="physical_health"
placeholder="Enter Physical Health"
v-model="form.physical_health"
required
>
<br> <br>
<input
type="text"
name="education"
placeholder="Enter Education"
v-model="form.education"
required
>
<br> <br>
<input
type="text"
name="mental_health"
placeholder="Enter Mental Health"
v-model="form.mental_health"
required
>
<br><br>
<input
type="text"
name="behaviour"
placeholder="Enter Behaviours"
v-model="form.behaviour"
required
>
<br><br>
<input
type="text"
name="self_care"
placeholder="Enter Self Care"
v-model="form.self_care"
required
>
<br><br>
<input
type="text"
name="life_skill"
placeholder="Enter Life Skills"
v-model="form.life_skill"
required
>
<br><br>
<input type="submit" value="Add Station" class="submit"> <br><br>
</form>
Vue 腳本代碼
<script setup>
import { useForm } from '@inertiajs/inertia-vue3';
const props = defineProps({
sData: String
});
const form = useForm({
hobbie: '',
physical_health: '',
education:'',
mental_health:'',
behaviour: '',
self_care: '',
life_skill: '',
});
const submit = () => {
form.post(route('add_station_1'), {
onFinish: () => form.reset(),
});
};
</script>
uj5u.com熱心網友回復:
您需要將隱藏欄位分配為 useForm 中的表單欄位
<input type="text" name="ChildID" v-model="form.child_id"
:value="data.ChildID">
</div>
const form = useForm({
hobbie: '',
physical_health: '',
education:'',
mental_health:'',
behaviour: '',
self_care: '',
life_skill: '',
child_id: sData[0].ChildID,
});
只有在表單陣列中設定的欄位才會與表單請求一起提交。如果提交后不清除此欄位,則需要在提交后重新填充該變數。
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/511474.html
