我需要在vuejs中运行类似jq的功能:
- html
@foreach($users as $user)
<td>
<selecter class="check-change" data-id="{{$user->id}}">
<opt @if($user->status == 'active') selected @endif value="active">Active</opt>
<opt @if($user->status == 'wait_confirm') selected @endif value="wait_confirm">Wait Confirm</opt>
<selecter>
</td>`
- jq
`$('.check-change').on('change', function() {
var new_value = this.value;
-> send request update user (this.attr('data-id'))
});
我被卡住了,因为我不知道如何在vue js中检查并添加选中的属性,也不知道当用户的选择器发生变化时如何获取用户id和选中的选项。
当前代码:
const items_status = [
{
state: 'Active',
abbr: 'active',
},
{
state: 'Wait Confirm',
abbr: 'wait_confirm',
}
];
const selectedOption = ref({
state: 'Wait Confirm',
abbr: 'wait_confirm',
})
<tr
v-for="user in users"
:key="user.id"
style="height: 3.75rem;"
<td>
<VSelect
v-model="selectedOption"
:hint="`${selectedOption.state}, ${selectedOption.abbr}`"
:items="items_status"
item-title="state"
item-value="abbr"
label="Select"
persistent-hint
return-object
single-line
/>
</td>
</tr> Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号
如果你想在选项改变时触发某些操作,那么你可以使用一个 watcher 来实现:
setup() { const options = [ ... ]; const selectedOption = Vue.ref({ ... }); Vue.watch(selectedOption, () => { // selectedOption 改变了 }); return { options, selectedOption }; }如果你想自定义 VSelect,就像你在第一个例子中展示的那样,那么你可以替换选项组件。但是我相信 Vuetify 已经处理了活动状态,所以除非你需要特定的设计,否则不需要修改它。
<v-select v-model="selectedOption" :items="options" item-title="state" item-value="abbr" label="Select" return-object single-line> <template #item="{ item, props }"> <v-list-item v-bind="{ ...props, title: undefined }"> {{ item.value.abbr === selectedOption.abbr ? 'selected' : '' }} {{ item.title }} </v-list-item> </template> </v-select>