我正在嘗試根據從下拉串列中選擇的國家/地區使用 ajax 和 php 執行 onchange 事件。我的問題是回應在每個專案的陣列中設定了最后一個價格,我想不出解決這個問題的方法。到目前為止,這是我的代碼:
$("#field-organization-country-iso").on('change', (e) => {
e.preventDefault();
e.stopPropagation();
const CountryIso = $('#field-organization-country-iso').val();
Request.get(`/myrequested_file`, {
data: { CountryIso },
})
.done((response) => {
for (let i = 0; i < response.data.itemPrice.length; i ) {
const price = response.data.itemPrice[i];
$('.checkout-table tr').find('.hide-if-odd-or-sub').eq(i).html(price);
}
});
});
和 php 函式:
public function change_currencyIso(Request $request, $summaryService): JSONResponse
{
$countryIso = $request->query->get('CountryIso');
$response = new JSONResponse();
$orderSummary = $summaryService->Summary(Cart::getCurrentCart(), User::currentUserOrNull(), $countryIso, null, null);
$items = $orderSummary->getItems();
$currencies = new ISOCurrencies();
$numberFormatter = new NumberFormatter(Environ::getLocale(), NumberFormatter::CURRENCY);
$moneyFormatter = new IntlMoneyFormatter($numberFormatter, $currencies);
$prices = [];
foreach ($items as $item) {
$price = $moneyFormatter->format($item->getLocalPrice());
$prices[] = $price;
}
$response->setVar('itemPrice', $prices);
return $response;
}
$prices 回傳帶有商品價格的陣列,但我知道回應會覆寫它。我可以遍歷陣列并將回應添加到每個價格嗎?我對“itemPrice”的回應僅回傳現有價格之一。
{itemPrice: Array(2)}
itemPrice: Array(2)
0: "245,00 US$"
1: "32,90 US$"
length: 2
現在 itemPrice 回傳陣列,但仍將所有內容放在同一行上。嘗試了一個,但沒有幫助。
uj5u.com熱心網友回復:
您應該添加有關 html 結構的更多詳細資訊,但是我想您有一個表格,每個專案都有一行,并且帶有價格的單元格具有“隱藏如果奇數或子”類。表行的順序也與服務器回傳的價格相同。因此,您必須將每個價格分配給相應的表格行:
foreach (var i in response.data.itemPrice) {
var price = response.data.itemPrice[i];
$('.checkout-table tr').eq(i).find('.hide-if-odd-or-sub').html(price);
}
這不是很可靠,因為如果用戶在另一個瀏覽器的選項卡中更改購物車中的專案并回傳上一個選項卡并更新國家/地區,那么服務器回傳的價格將與表中的專案不對應,但是它應該可以正常使用。
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/314700.html
上一篇:將jquery.getJSON轉換為ajax異步等待YouTubeAPI
下一篇:如何將資料系結到甜甜圈劍道組件
