First off using jQuery with Vue is frowned apon, You are using a dom manipulation library with a dom rendering library, You can do this much simpler.
(首先,将jQuery与Vue结合使用会让人皱眉,您正在使用带有dom渲染库的dom操作库,您可以做到这一点更加简单。)
By using v-if you can set the element to show if a variable exists, You can also do this with the disabled attribute via :disabled="variable"
(通过使用v-if,您可以设置元素以显示变量是否存在,也可以通过:disabled="variable"
对disable属性进行操作)
<template>
<section>
<div class="form-group">
<label>Asset Depreciation Method<span class="required-field">*</span></label>
<select class="form-control" name="asset_depreciation" required="required" @change="assetDepreciation" v-model="asset_depreciation">
<option value="0">A</option>
<option value="1">B</option>
<option value="2">C</option>
<option value="3">D</option>
</select>
</div>
<div class="form-group periods">
<input v-if="show_input" type="text" class="form-control" name="period" required="required">
</div>
</section>
</template>
<script>
export default {
data: () => ({
modal_title: 'Add Asset',
asset_code: '',
coa_checker_result: '',
asset_depreciation: '',
show_input: false,
}),
ready()
{
this.checkAssetCode();
this.assetDepreciation();
this.createData()
},
components: {
},
//controller
methods: {
checkAssetCode(e) {
e.preventDefault();
var code = this.asset_code;
const asset = this.$refs.assetCode
const table = asset.dataset.table
axios.post("/checkIfCodeExists", {code:code,table:table})
.then((response) => {
var code_checker = '';
if (response.data == 0) {
$('.add-asset').removeAttr('disabled','disabled');
// code_checker = 'wala pang ganitong code';
}else{
$('.add-asset').attr('disabled','disabled');
code_checker = 'Code is already exist';
}
this.coa_checker_result = code_checker;
});
},
assetDepreciation(e){
e.preventDefault();
var value = this.asset_depreciation;
/* UNIT OF PRODUCTION*/
if(value == 2)
{
alert(value);
this.show_input = true;
}
},
}
}
Since you have the select bound using v-model you dont need the assetDepreciation method either, you can do a <input v-if="asset_depreciation == 2" type="text" class="form-control" name="period" required="required">
(由于您具有使用v模型的选择范围,因此也不需要assetDepreciation方法,因此您可以执行<input v-if="asset_depreciation == 2" type="text" class="form-control" name="period" required="required">
)
Below is what I would do.
(下面是我会做的。)
<template>
<section>
<div class="form-group">
<label for="asset_depreciation">
Asset Depreciation Method<span class="required-field">*</span>
</label>
<select
id="asset_depreciation"
class="form-control"
name="asset_depreciation"
required="required"
v-model="asset_depreciation"
>
<option value="0">A</option>
<option value="1">B</option>
<option value="2">C</option>
<option value="3">D</option>
</select>
</div>
<button :disabled="button_disabled" @click="someMethod">Add Asset</button>
<div class="form-group periods">
<input
v-if="asset_depreciation == 2"
type="text"
class="form-control"
name="period"
required="required"
/>
</div>
</section>
</template>
<script>
export default {
data() {
return {
modal_title: "Add Asset",
asset_code: "",
button_disabled: true,
coa_checker_result: "",
asset_depreciation: ""
};
},
mounted() {
this.checkAssetCode();
this.createData();
},
methods: {
someMethod() {
// yada yada
},
checkAssetCode(e) {
e.preventDefault();
var code = this.asset_code;
const asset = this.$refs.assetCode;
const table = asset.dataset.table;
axios
.post("/checkIfCodeExists", { code: code, table: table })
.then(response => {
this.button_disabled = response.data != 0;
});
},
}
};