gpt4 book ai didi

javascript - 在 Vue 中使用条件问题创建动态表单

转载 作者:行者123 更新时间:2023-12-01 02:25:47 25 4
gpt4 key购买 nike

但是,我想使用条件字段动态创建表单。形式的定义在对象 Q 中。下面的示例是一个使用 bootstrap-vue 的 Vue 组件。

<template>
<div>
<div v-for="q of Q">
<br/>
<template v-if="q.type == 'input'">
{{ q.question }}
<em v-if="q.comment"><br />{{ q.comment }}</em>
<b-form-input v-model="q.value" :type="q.subtype" :placeholder="q.placeholder"></b-form-input>
Value: {{ q.value }}
</template>

<template v-if="q.type == 'radio'">
{{ q.question }}
<em v-if="q.comment"><br />{{ q.comment }}</em>
<b-form-group>
<b-form-radio-group buttons
stacked
v-model="q.value"
:options="q.options"/>
</b-form-group>

Value: {{ q.value }}
</template>

</div>
</div>
</template>

<script>
export default {
name: 'Questionnaire',
data() {
return {
locale: 'en',
Q: [
{
name: 'age',
value: null,
question: 'How old are you?',
placeholder: 'Your age...',
comment: 'years since you were born',
type: 'input',
subtype: 'number',
range: [18, 99],
},
{
name: 'cq',
value: null,
question: 'Conditional Question?',
type: 'radio',
options: [
{text: 'Yes', value: '0'},
{text: 'No', value: '1'},
],
if: [{object: 'age', largerthan: 30}],
},
]
};
},
methods: {
onChange: function(){
alert('test');
},
},
}
</script>

我只想在年龄 > 30 岁的情况下显示“条件问题”。

  • 在对象 Q 中,我无法访问 this.Q (因为它还不存在)。
  • v-on:change="onChange"可以工作,但这违背了 Vue 的全部意义

我不受该对象结构的约束,但它将使用 AJAX 获得......

问题:有没有办法观看this.Q[0].value?或者只有当第一个问题具有一定值时才可以使用第二个问题?

最佳答案

我通过在模板的第二个 div 上使用“v-if”指令成功创建了效果。

然后我用空数组初始化了“Q”数组,并在“created()”生命周期钩子(Hook)中通过setTimeout模拟了AJAX请求。

<template>
<div>
<div v-if="!(q.if) || Q[0].value > q.if[0].largerthan" v-for="q of Q">
<br/>
<template v-if="q.type == 'input'">
{{ q.question }}
<em v-if="q.comment"><br />{{ q.comment }}</em>
<b-form-input v-model="q.value" :type="q.subtype" :placeholder="q.placeholder"></b-form-input>
Value: {{ q.value }}
</template>

<template v-if="q.type == 'radio'">
{{ q.question }}
<em v-if="q.comment"><br />{{ q.comment }}</em>
<b-form-group>
<b-form-radio-group buttons
stacked
v-model="q.value"
:options="q.options"/>
</b-form-group>

Value: {{ q.value }}
</template>

</div>
</div>
</template>

<script>
export default {
name: 'Questionnaire',
data() {
return {
locale: 'en',
Q: [],
};
},
created() {
setTimeout( _ => this.Q = [
{
name: 'age',
value: null,
question: 'How old are you?',
placeholder: 'Your age...',
comment: 'years since you were born',
type: 'input',
subtype: 'number',
range: [18, 99],
},
{
name: 'cq',
value: null,
question: 'Conditional Question?',
type: 'radio',
options: [
{text: 'Yes', value: '0'},
{text: 'No', value: '1'},
],
if: [{object: 'age', largerthan: 30}],
},
{
name: 'age',
value: null,
question: 'How old are you?',
placeholder: 'Your age...',
comment: 'years since you were born',
type: 'input',
subtype: 'number',
range: [18, 99],
},
], 500)
},
}

关于javascript - 在 Vue 中使用条件问题创建动态表单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48826354/

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