gpt4 book ai didi

javascript - 使用 @click 方法和 select 标签来更改计算属性的值

转载 作者:行者123 更新时间:2023-11-28 03:32:06 24 4
gpt4 key购买 nike

我想做的是:

  1. 使用文本框输入情绪,并使用选择框评价情绪的强度。蓝色按钮增添了情感。
  2. 在重新评估部分,我想重新评估这种情绪的强度。

在重估中,我循环遍历计算属性 rerateEmotions有一个对象数组,其中包含 emotionintensity特性。我想做的是使用 select 标签更改计算属性上每种情绪的强度:

编辑

好吧,我发现我需要更改 @click@change关于重新定价<select>

现在我只想弄清楚如何从“情绪评级”和“重新评级情绪”区域中进行单独的评级。

const app = new Vue({
el: "#app",
data: function() {
return {
emotion: "",
intensity: null,
newIntensity: null,
emotionIntensity: []
};
},
computed: {
rerateEmotions() {
return this.emotionIntensity;
}
},
methods: {
rerateIntensity(id) {
return (this.rerateEmotions[id].intensity = this.newIntensity);
},
openEmotion(id) {
this.emotionId = id;
},
addEmotionIntensity() {
var emotionIntensity = {
emotion: this.emotion,
intensity: this.intensity
};
this.emotionIntensity.push(emotionIntensity);
this.emotion = "";
this.intensity = null;
},
removeEmotion(id) {
if (id > -1) {
this.emotionIntensity.splice(id, 1);
}
},
}
});
<link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.1.3/css/bootstrap.min.css" rel="stylesheet" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<div id="app">

<div class="row mb-3 pr-4">
<div class="col-6">
<h1>Rate</h1>
<input v-model="emotion" type="text" placeholder="Enter emotion here." class="form-control" id="basic-url" aria-describedby="basic-addon3">
</div>
<div class="col-5 select-outline">
<select class="mdb-select md-form md-outline colorful-select dropdown-primary" v-model="intensity">
<option value="" disabled selected>Rate Emotion</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
<option value="7">7</option>
<option value="8">8</option>
<option value="9">9</option>
<option value="10">10</option>
</select>
</div>
<button @click="addEmotionIntensity()" type="button" class="col-1 btn btn-primary">+</button>
</div>

<ul>
<li v-for="(emotion, index) in emotionIntensity">
<b>{{emotion.emotion}} : {{emotion.intensity}}</b>
<button type="button" @click="removeEmotion(index)">
X
</button>
</li>
</ul>

<hr>

<div class="form-group">
<label for="exampleFormControlTextarea1"><h5>Rerate</h5></label>

<li v-for="(emotion, index) in rerateEmotions">
<b>{{emotion.emotion}} : {{emotion.intensity}}</b>
<select class="mdb-select md-form md-outline colorful-select dropdown-primary" @change="rerateIntensity(index)">
<option value="" disabled selected>Rate Emotion</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
<option value="7">7</option>
<option value="8">8</option>
<option value="9">9</option>
<option value="10">10</option>
</select>
</li>
<hr>
</div>
</div>

最佳答案

如果您需要两个强度等级,那么每个对象可以只具有两个属性。例如强度newIntensity。在下面的示例中,我在首次创建条目时将这两个属性初始化为相同的值,但如果愿意,第二个属性可以保留为空。

计算属性 rerateEmotions 似乎没有执行任何操作,因此我将其删除,转而直接使用 emotionIntensity。也许目的是创建一个副本,但使用计算属性不会产生这种效果,它只是同一个数组。

对于重新评级下拉列表,我使用了 v-model,不过如果您愿意的话,没有理由不使用事件监听器。关键是我使用 newIntensity 属性作为第二个值。

const app = new Vue({
el: "#app",
data: function() {
return {
emotion: "",
intensity: null,
newIntensity: null,
emotionIntensity: []
};
},
methods: {
openEmotion(id) {
this.emotionId = id;
},
addEmotionIntensity() {
var emotionIntensity = {
emotion: this.emotion,
intensity: this.intensity,
newIntensity: this.intensity
};
this.emotionIntensity.push(emotionIntensity);
this.emotion = "";
this.intensity = null;
},
removeEmotion(id) {
if (id > -1) {
this.emotionIntensity.splice(id, 1);
}
},
}
});
<link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.1.3/css/bootstrap.min.css" rel="stylesheet" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<div id="app">

<div class="row mb-3 pr-4">
<div class="col-6">
<h1>Rate</h1>
<input v-model="emotion" type="text" placeholder="Enter emotion here." class="form-control" id="basic-url" aria-describedby="basic-addon3">
</div>
<div class="col-5 select-outline">
<select class="mdb-select md-form md-outline colorful-select dropdown-primary" v-model="intensity">
<option value="" disabled selected>Rate Emotion</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
<option value="7">7</option>
<option value="8">8</option>
<option value="9">9</option>
<option value="10">10</option>
</select>
</div>
<button @click="addEmotionIntensity()" type="button" class="col-1 btn btn-primary">+</button>
</div>

<ul>
<li v-for="(emotion, index) in emotionIntensity">
<b>{{emotion.emotion}} : {{emotion.intensity}}</b>
<button type="button" @click="removeEmotion(index)">
X
</button>
</li>
</ul>

<hr>

<div class="form-group">
<label for="exampleFormControlTextarea1"><h5>Rerate</h5></label>

<li v-for="(emotion, index) in emotionIntensity">
<b>{{emotion.emotion}} : {{emotion.newIntensity}}</b>
<select v-model="emotion.newIntensity" class="mdb-select md-form md-outline colorful-select dropdown-primary">
<option value="" disabled selected>Rate Emotion</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
<option value="7">7</option>
<option value="8">8</option>
<option value="9">9</option>
<option value="10">10</option>
</select>
</li>
<hr>
</div>
</div>

关于javascript - 使用 @click 方法和 select 标签来更改计算属性的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58091450/

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