所以我有這個JavaScript函數循環通過每個用戶帳戶,並顯示在下拉菜單中。當用戶從下拉菜單中選擇一個選項時,它將Iban編號作爲其主ID存儲在ddlAccountFrom中。有沒有辦法在用戶選擇一個選項時如何存儲兩個值,例如Iban和貨幣到單獨的變量中?如何在變量內存儲多個下拉值?
function getAccountFrom() {
var username = $("#hiddenUsername").val();
var url = "http://localhost:63723/api/BankAccountsApi/GetBankAccounts/?username=" + username + "&id=2";
$.getJSON(url, function (data) {
var table = "<select id=\"ddlAccountFrom\">";
table += "<option value=\"-1\">Select an Account</option>";
$.each(data, function (key, val) {
table += "<option value=\"" + val.Iban + "\">" + val.Iban + " | " + val.Currency + " | " + val.Balance + "</option>";
});
table += "</select>";
$('#divAccountFrom').html(table);
});
}
我在這個函數中使用ddlAccountFrom ..
function Transfer() {
var accountFrom = $("#ddlAccountFrom").val();
var accountTo = $("#txtAccountTo").val();
var amount = $("#txtAmount").val();
var url = "http://localhost:63723/api/BankAccountsApi/TransferFunds/?
ibanFrom=" + accountFrom + "&ibanTo=" + accountTo + "&amount=" + amount;
$.getJSON(url, function (data) {
alert(data);
})
.error (function() {
alert("Not enough balance! The amount entered exceed the balance found in this account. Please try again.");
})
}
那麼,你應該在用戶選擇一個選項時顯示你的代碼,但是通常你可以使用分隔符連接這些值並將其放入選項的'value'中。 –