我正在 Vuejs/Nuxtjs 中动态创建一些 datetime-local 类型的输入字段。我想将所有这些输入字段的默认值设置为 2022-04-21T14:27:27.000。怎么做?
对于直接字段,我们可以使用 v-model 分配值,但我有点不确定如何为 datetime-local 的所有字段设置默认值。如果我没记错的话,Vanilla JavaScript 中有一个选项可以获取某种类型的所有字段并更改值,有没有办法使用 Vuejs/Nuxtjs 来实现相同的目的。
以下是我到目前为止的代码:
<template>
<div>
<button type="button" @click="addField" class="btn btn-info">
Add Field
</button>
<div v-for="field in fieldArray" :key="field.ID" class="form-group">
<input
v-model="field.time"
type="datetime-local"
class="form-control"
step="1"
value="2022-04-21T14:27:27.000"
@change="timeChange(field.ID)"
/>
</div>
</div>
</template>
<script>
export default {
data() {
return {
fieldCount: 0,
fieldArray: [],
};
},
mounted() {
let now = new Date();
now.setMinutes(now.getMinutes() - now.getTimezoneOffset());
now.setSeconds(now.getSeconds(), 0);
now = now.toISOString().slice(0, -1);
//I would like to set this "now" time value for all the created fields of input type datetime-local
console.log("Current Time : " + now);
},
methods: {
//Add field
addField() {
const fieldObj = { ID: this.fieldCount, time: "" };
this.fieldArray.push(fieldObj);
},
//Time change
timeChange(ID) {
console.log("ID : " + ID);
console.log(JSON.stringify(this.fieldArray, null, 4));
},
},
};
</script>
<style>
</style>
我想要做的就是为我的组件中的所有 datetime-local 字段设置默认时间。有什么办法可以做到吗?我可以对单个字段实现此目的,但不能对动态创建的字段实现此目的。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号
类似这样的吗?