如何通过值选择列表框中的选项

时间:2019-06-19 18:49:00

标签: jquery select

我想用值来动态选择一个选项,然后用jquery填充一个列表框。但是我还没有成功。

这是清单

在页面加载时填充列表

    function getOpenBilllist() {
        $(function () {
            $.ajax({
                type: "POST",
                url: "businessLounge.aspx/getOpenBillList",
                data: {},
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function (r) {
                    var openbillList = $('.openBill');
                    openbillList.empty();
                    $.each(r.d, function () {
                        openbillList.append($("<option></option>").val(this['Value']).html(this['Text']));
                    });
                },
                error: function () {
                    alert("Error");
                }
            });
        });
    }

脚本:此方法将文章添加到帐单和getOpenBilllist();方法重新填充列表,以获取未结汇票的最新列表。

    function addArticleToBill(articleID) {
        $(function () {
            var uid = $("#<% = uID.ClientID %>").val();
            var contactPersonID = getContactPersonID();
            if (typeof contactPersonID == "undefined") {
                alert("Please Select Contact Person");
            } else {
                $.ajax({
                    type: "POST",
                    url: "businessLounge.aspx/addArticleInBill",
                    data: "{'articleID':'" + articleID + "','contactPersonID':'" + contactPersonID + "','uid':'" + uid + "'}",
                    contentType: "application/json; charset=utf-8",
                    dataType: "json",
                    success: function (r) {
                        getBill(contactPersonID, '');
                        getOpenBilllist();
                        $('.openBill').val(contactPersonID);
                    },
                    error: function () {
                        alert("Error");
                    }
                });
            }
        });
    }

问题:

重新填充列表后成功,我想在列表中选择选项$('。openBill')。val(contactPersonID);但未选择选项。选项在列表中。我不知道我的代码没有选择它。调试窗口中也没有错误。

预先感谢

1 个答案:

答案 0 :(得分:0)

由于 $。ajax()是异步的,因此您不能在调用 getOpenBilllist(); 之后选择该选项。

相反,您可以将值传递给函数,然后在此处选择选项:

/*
function getOpenBilllist(contactPersonID) {
    $.ajax({
        type: "POST",
        url: "businessLounge.aspx/getOpenBillList",
        data: {},
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (r) {
            var openbillList = $('.openBill');
            openbillList.empty();
            $.each(r.d, function () {
                openbillList.append($("<option></option>").val(this['Value']).html(this['Text']));
            });
            openbillList.val(contactPersonID); // <-------------------
        },
        error: function () {
            alert("Error");
        }
    });
}
*/

var openbillList = $('.openBill');
contactPersonID = 2;
openbillList.empty();
$.each([{Value: 1, Text: 1}, {Value: 2, Text: 2}, {Value: 3, Text: 3}], function () {
    openbillList.append($("<option></option>").val(this['Value']).html(this['Text']));
});
$('.openBill').val(contactPersonID);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>


<select class="openBill"></select>

相关问题