Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/256.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/78.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 回发时无法将hiddenfield值分配给select2_C#_Jquery_Asp.net_Jquery Select2 - Fatal编程技术网

C# 回发时无法将hiddenfield值分配给select2

C# 回发时无法将hiddenfield值分配给select2,c#,jquery,asp.net,jquery-select2,C#,Jquery,Asp.net,Jquery Select2,我试图从客户端脚本中的hiddenfield为select2控件赋值。回发以下代码后,未将值分配给select2控件 $(document).ready(function () { $("#cboIndustry").select2(); $.getJSON(uriSector+ '/' + 'GetIndustrySectors') .done(function (data) { $.each(data, functio

我试图从客户端脚本中的hiddenfield为select2控件赋值。回发以下代码后,未将值分配给select2控件

$(document).ready(function () {
     $("#cboIndustry").select2();

     $.getJSON(uriSector+ '/' + 'GetIndustrySectors')
           .done(function (data) {
               $.each(data, function (key, item) {
                  $("#cboIndustry").append($("<option></option>").val(item.IndustrySectorID).html(item.IndustrySectorName));
               });
           });

     $("#cboIndustry").on('change', function () {
            if ($("#cboIndustry").val() != "-1") {

                 var id = $("#cboIndustry").val();
                $('#HiddenIndustrySectorID').val(id);
                SelectedName = $('#cboIndustry option:selected').text();
                $('#HiddenIndustrySectorName').val(SelectedName);
            }
        });

   var SelectedIndustry = $('#HiddenIndustrySectorID').val();

   $("#cboIndustry").select2().select('val',SelectedIndustry);

});

原因可能是什么?请帮帮我。

为什么不用这条线

$("#cboIndustry").select2().val(SelectedIndustry); 
顺便说一句,我还没有测试

 $('#HiddenIndustrySectorID').val(id);
把这行改成

document.getElementById("HiddenIndustrySectorID").value =id;
然后尝试

$(文档).ready(函数(){
$(document).ready(function () {
    $("#cboIndustry").select2();

    $.getJSON(uriSector+ '/' + 'GetIndustrySectors')
       .done(function (data) {
           $.each(data, function (key, item) {
              $("#cboIndustry").append($("<option></option>").val(item.IndustrySectorID).html(item.IndustrySectorName));
        });
    //This change solves my problem

    var SelectedIndustry = $('#HiddenIndustrySectorID').val();

    $("#cboIndustry").select2().select('val',SelectedIndustry);

 });

 $("#cboIndustry").on('change', function () {
        if ($("#cboIndustry").val() != "-1") {
            var id = $("#cboIndustry").val();
            $('#HiddenIndustrySectorID').val(id);
            SelectedName = $('#cboIndustry option:selected').text();
            $('#HiddenIndustrySectorName').val(SelectedName);
        }
    });
 });
$(“#cBoinIndustry”)。选择2(); $.getJSON(uriSector++'/'+'GetIndustrySectors') .完成(功能(数据){ $。每个(数据、功能(键、项){ $(“#cboIndustry”).append($(“”).val(item.IndustrySectorID.html(item.IndustrySectorName)); }); //这个改变解决了我的问题 var SelectedIndustry=$('HiddenIndustrySectorID').val(); $(“#cBoinIndustry”).select2().select('val',SelectedIndustry); }); $(“#cboIndustry”)。on('change',function(){ 如果($(“#cboIndustry”).val()!=“-1”){ var id=$(“#cboIndustry”).val(); $('HiddenIndustrySectorID').val(id); SelectedName=$('#cBoinIndustry选项:selected')。text(); $('HiddenIndustrySectorName').val(SelectedName); } }); });
$(document).ready(function () {
    $("#cboIndustry").select2();

    $.getJSON(uriSector+ '/' + 'GetIndustrySectors')
       .done(function (data) {
           $.each(data, function (key, item) {
              $("#cboIndustry").append($("<option></option>").val(item.IndustrySectorID).html(item.IndustrySectorName));
        });
    //This change solves my problem

    var SelectedIndustry = $('#HiddenIndustrySectorID').val();

    $("#cboIndustry").select2().select('val',SelectedIndustry);

 });

 $("#cboIndustry").on('change', function () {
        if ($("#cboIndustry").val() != "-1") {
            var id = $("#cboIndustry").val();
            $('#HiddenIndustrySectorID').val(id);
            SelectedName = $('#cboIndustry option:selected').text();
            $('#HiddenIndustrySectorName').val(SelectedName);
        }
    });
 });