我正在使用Laravel 5.7VueJs 2.5.* ...

我想在我的TotalFares中获取所有票价的总和。我当时在玩,但没有取得任何成功...

这是我的Fares <Input>



<tr v-for="(ticketInvoiceItem, key) in form.ticketInvoiceItems" :key="key">
  <td>
    <input v-model.number="ticketInvoiceItem.fares" type="number" size="10" name="fares" class="table-control form-control" :class="{ 'is-invalid': form.errors.has('fares') }">
    <has-error :form="form" field="fares"></has-error>
  </td>
</tr>



这是我的TotalFares <input>



<tfoot class="tfoot">
  <tr>
    <td class="table-amount">

      <input :value="getFareTotal()" type="text" name="ticket_invoice_fares_total" class="form-control" :class="{ 'is-invalid': form.errors.has('ticket_invoice_fares_total') }">

    </td>
  </tr>
</tfoot>



我在data()中的VueJs



data() {
  return {
    ticketInvoices: {},
    vendors: null,
    form: new Form({
      id: "",
      vendor_id: "",
      ticket_invoice_no: "",
      ticket_invoice_date: "",
      ticket_invoice_fares_total: "",

      ticketInvoiceItems: [{
        id: "",
        ticket_invoice_id: "",
        passenger_name: "",
        fares: 0,
      }]
    })
  }
},



我试图得到FaresTotal类似的东西,但失败了:



getFareTotal() {
  let calFareTotal =
    parseInt(this.form.ticketInvoiceItems[key].fares) +
    parseInt(this.form.ticketInvoice.ticket_invoice_fares_total);

  this.form.ticketInvoice.ticket_invoice_fares_total = calFareTotal;

  return calFareTotal;
},

最佳答案

getFareTotal() {
  let sum = 0
  this.form.ticketInvoiceItems.forEach(i=> sum+=i.fares);
  console.log(sum)
}


ticketInvoiceItems是一个集合。因此,您必须对此进行迭代。

关于javascript - 如何获得所有票价的总和,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/53017209/

10-11 03:26