簡體   English   中英

表單輸入字段之間的計算

[英]Calculations between form input fields

我想尋求一點幫助。 我是Vue的初學者,但是做一個簡單的計算器。

問題是:輸入了兩個值的三種形式並在第三種形式進行計算。

最好我將所有三個都留空,當將數據確定為兩個時,將在第三個中進行計算。 哪兩個都沒關系。 不幸的是,我無法弄清楚,所以我選擇了兩個特定值,然后使用compute在第三位進行計算。 我希望這樣,以便無論您輸入哪兩個,第三個都會顯示我需要的計算。

此外,您可以從代碼中看到多個if語句。 我敢肯定有一種更好的方法來編寫此代碼。 歸根結底,我的代碼可以運行,但是當使用多個我需要的if語句時,它將變得很長。

new Vue({
  el: "#app",
  data: {
    value1: null,
    value2: null
  },
  computed: {
    a_ratio() {
      var a_number = parseInt(this.value2) / parseInt(this.value1);
      return a_number.toFixed(1);
    },

    calculation() {
      if (this.value1 < 100 && this.value2 > 0) {
        var val = parseInt(this.value2) / 10 || 0;
        return val.toFixed(1);
      }
      if (this.value1 >= 100 && this.value2 > 0) {
        var val = parseInt(this.value2) / 20 || 0;
        return val.toFixed(1);
      }
    }
  }
});

這么漂亮:)

new Vue({
  el: "#app",
  data: {
    value1: null,
    value2: null
  },
  computed: {
    a_ratio() {
      var a_number = parseInt(this.value2) / parseInt(this.value1);
      return a_number.toFixed(1);
    },

    calculation() {
      if (this.value2 <= 0) return;
      let divider;

      if (this.value1 < 100) {
        divider = 10;
      } else if (this.value1 >= 100) {
        divider = 20;
      }

      return (parseInt(this.value2) / divider || 0).toFixed(1);
    }
  }
});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM