【发布时间】:2021-03-12 13:42:06
【问题描述】:
我正在使用 Vue.js 3 并尝试在 ajax 发布期间禁用表单元素,然后在发布完成后再次启用它们(无论成功与否)。
开始提交时将 isFormSubmitting 属性设置为 true。我遇到的问题是将其设置回 false 会使元素禁用。
这是我绑定到 disabled 属性的方式
<textarea v-bind:disabled='isFormDisabled' v-bind:value="model.description" class="form-control" rows="6"></textarea>
这是脚本:
<script type="text/javascript">
const App = {
data() {
return {
model: @(Html.Raw(Model.Json)),
isFormSubmitting: false
}
},
computed: {
isFormDisabled: function () {
return this.isFormSubmitting;
}
},
methods: {
processData() {
this.isFormSubmitting = true;
var json = JSON.stringify(this.model);
console.log('isFormDisabled = ' + this.isFormDisabled);
$.post(
{
url: '@Url.Action("SaveNewTicket", "DriverTickets")',
data: json,
success: function ()
{
console.log('submitted');
this.isFormSubmitting = false;
console.log('isFormDisabled = ' + this.isFormDisabled);
},
fail: function ()
{
console.log('failed');
this.isFormSubmitting = false;
console.log('isFormDisabled = ' + this.isFormDisabled);
},
done: function ()
{
console.log('done');
this.isFormSubmitting = false;
console.log('isFormDisabled = ' + this.isFormDisabled);
}
});
},
}
}
const app = Vue.createApp(App);
const vm = app.mount('#app');
</script>
这是我的控制台输出:
isFormDisabled = true
submitted
isFormDisabled = undefined
我也尝试过直接绑定到一个属性。当我这样做时,我从输出中得到 false 而不是 undefined,但 disabled 属性仍保留在表单上。
诚然,我不是一个很好的前端开发人员,因此也欢迎与问题本身无关的批评。
【问题讨论】:
-
你的
$post回调函数应该是arrow functions
标签: javascript vuejs3