如何在变量中存储多个下拉值?

时间:2016-04-28 15:27:31

标签: javascript jquery

所以我有这个javascript函数循环遍历每个用户帐户并在下拉菜单中显示它们。当用户从下拉菜单中选择一个选项时,它将Iban编号作为其主ID,存储在ddlAccountFrom中。有没有办法在用户选择一个选项时如何存储两个值,例如Iban和Currency分别变量?

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.");
    })

}

2 个答案:

答案 0 :(得分:1)

您可以使用data-custom属性,如下所示:

table += "<option value=\"" + val.Iban + "\" data-currency=\"" + val.Currency + "\">" + val.Iban + " | " + val.Currency + " | " + val.Balance + "</option>";

要访问变量,请参阅jquery-cant-get-data-attribute-value 所以你可以阅读:

var selectedCurrency= $("#ddlAccountFrom :selected").data('currency');

答案 1 :(得分:0)

你可以:

  1. 将1个字符串中的2个数据与分隔符(对于前ORIENTDB_OPTS_MEMORY="-Xms128m -Xmx256m")连接,然后拆分您的值以获取2个不同的数据
  2. 收听您的下拉列表更改,例如在您的选项html和js中添加myIban#myCurrency属性:

    onchange=updateData(val.Iban, val.Currency)
  3. 添加数据自定义属性,例如data-currency或data-iban