Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
459 views
in Technique[技术] by (71.8m points)

javascript - Laravel 5:追加输入文本取决于选定的选项vue js(Laravel 5: Append input text depends on the selected option vue js)

In our application, we have select option.

(在我们的应用程序中,我们有选择选项。)

For example in my select option I have 4 values: A, B, C and D. When I select the option C, it will add another input text.

(例如,在我的选择选项中,我有4个值:A,B,C和D。当我选择选项C时,它将添加另一个输入文本。)

Vue

(Vue)

<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">
</div>

Script

(脚本)

data: () => ({
        modal_title: 'Add Asset',
        asset_code: '',
        coa_checker_result: '',
        asset_depreciation: '',
    }),
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);
                    $('.periods').append(''+
                        '<input type="text" class="form-control" name="period" required="required>"'
                    );
                }

            },

When I selected C, alert will show but it didn't append in my class periods.

(当我选择C时,会显示警报,但警报不会在我的课堂上出现。)

Question: How do I append my input field depends on the selected option?

(问题:如何根据所选选项附加输入字段?)

  ask by Angel translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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;
        });
    },
  }
};



与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...