通过存储过程中的ajax值填充下拉列表

时间:2019-07-17 10:11:20

标签: javascript html ajax asp.net-mvc

我通过存储过程的url返回正确的ajax值。我只需要通过脚本填充我的下拉列表。但这不起作用,我认为我的脚本是错误的。

<script>
    function signatoriesdiv() {
        var selectsignatoriesdiv;
        $.ajax({
            url: servername + appName + 'Map/getAllSignatories',
            datatype: 'json',
            type: 'GET',
            success: function (data) {
                $("#verifytmcrpre").empty();
                for (var i = 0; i < data.length; i++) {
                    selectsignatoriesdiv += "<option value='" + data[i].SIGNATORIES_POSITION + "'>" + data[i].SIGNATORIES_NAME + "</option>";
                    $("#verifyt`enter code here`mcrpre").append(selectsignatoriesdiv);
                }
            },
            complete: function () {
                $("#signatoriesdiv").modal();
            }

        });
    };

`

<div id="signatoriesdiv">
  <label for="verifytmcrpre">Prepared by: </label>
  <select class="form-control" id="verifytmcrpre" name="verifytmcr" style="width: 100%;"> </select>

->这是我的网址返回的内容。

[{"ExtensionData":{},"SIGNATORIES_ID":"1","SIGNATORIES_NAME":"admin                                                                                                                                                                                                                                             ","SIGNATORIES_POSITION":"ADMIN                                                                                                                                                                                                                                              ","SIGNATORIES_STATUS":"Active                                                                                                                                                                                                                                                         "},{"ExtensionData":{},"SIGNATORIES_ID":"2","SIGNATORIES_NAME":"chief                                                                                                                                                                                                                                             ","SIGNATORIES_POSITION":"CHIEF                                                                                                                                                                                                                                     ","SIGNATORIES_STATUS":"Active                                                                                                                                                                                                                                                         "},{"ExtensionData":{},"SIGNATORIES_ID":"3","SIGNATORIES_NAME":"assesor                                                                                                                                                                                                                                    ","SIGNATORIES_POSITION":"assess                                                                                                                                                                                                                                                 ","SIGNATORIES_STATUS":"Active                                                                                                                                                                                                                                                         "}]

1 个答案:

答案 0 :(得分:0)

例如,您需要首先解析响应数据。

function loadVal() {
    var selectsignatoriesdiv ="";
    var data ='[{"ExtensionData":{},"SIGNATORIES_ID":"1","SIGNATORIES_NAME":"admin","SIGNATORIES_POSITION":"ADMIN","SIGNATORIES_STATUS":"Active"},{"ExtensionData":{},"SIGNATORIES_ID":"2","SIGNATORIES_NAME":"chief","SIGNATORIES_POSITION":"CHIEF","SIGNATORIES_STATUS":"Active"},{"ExtensionData":{},"SIGNATORIES_ID":"3","SIGNATORIES_NAME":"assesor","SIGNATORIES_POSITION":"assess","SIGNATORIES_STATUS":"Active "}]';
    var data=jQuery.parseJSON(data);       
	for (var i = 0; i < data.length; i++) {
        selectsignatoriesdiv = "<option value='" + data[i].SIGNATORIES_POSITION + "'>" + data[i].SIGNATORIES_NAME + "</option>";
        $("#verifytmcrpre").append(selectsignatoriesdiv);
        }
}
<head>
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
</head>
<body>
  <div id="signatoriesdiv">
  <label for="verifytmcrpre">Prepared by: </label>
  <select class="form-control" id="verifytmcrpre" name="verifytmcr" > </select>
  <button type="button" onclick="loadVal()">Click</button>
</body>

将此代码添加到成功功能中

$("#verifytmcrpre").empty();
var data=jQuery.parseJSON(data);       
for (var i = 0; i < data.length; i++) {
    selectsignatoriesdiv = "<option value='" + data[i].SIGNATORIES_POSITION + "'>" + data[i].SIGNATORIES_NAME + "</option>";
    $("#verifytmcrpre").append(selectsignatoriesdiv);
}