【问题标题】:Unable to set value to select2 drop down无法将值设置为 select2 下拉菜单
【发布时间】:2021-02-15 08:45:22
【问题描述】:

待办事项:在国家/地区下添加一个城市。然后尝试编辑城市记录。 我有:城市名称作为文本框,国家作为引导模式中的 Select2 下拉菜单,可使用页面上的“编辑”按钮访问。

问题 - 我无法在使用 ajax 调用从数据库检索的 select2 下拉列表中设置国家/地区的值。我尝试了所有可能的组合来设定价值。如果它是一个普通的下拉框,我可以设置值,但当它是一个 select2 框时则不能。

这是引导程序中选择 2 框的 html

<div class="modal fade" id="edit">
<div class="modal-dialog">
    <div class="modal-content">
        <div class="modal-header">
          <button type="button" class="close" data-dismiss="modal" aria-label="Close" onclick = "location.reload(true);">
              <span aria-hidden="true">&times;</span></button>
          <h4 class="modal-title"><b>Edit City</b></h4>
        </div>
        <div class="modal-body">
          <form class="form-horizontal" method="POST" action="city_edit.php">
            <input type="hidden" class="catid" name="id">
            <input type="text" name="cityid" id="cityid" hidden>
            <div class="form-group">
                <label for="name" class="col-sm-3 control-label">City</label>
                <div class="col-sm-8">
                  <input type="text" class="form-control" id="cityname" name="cityname" required>
                </div>
            </div>
            
            <div class="form-group">
                <label class="col-md-3 control-label">Country</label>
                <div class="col-md-8">
                    <select data-plugin-selectTwo class="form-control" name="country" id="country" required>
                            <option value = "">Select Country</option>
                            
                            <?php
                                $conn = $pdo->open();

                                try{
                                  $stmt = $conn->prepare("SELECT country_id,country FROM country where del_flg = 'N' and status = 1");
                                  $stmt->execute();
                                  foreach($stmt as $row)
                                  {
                                      $countryid = $row['country_id'];  
                                    $country = $row['country']; 
                                    
                                    echo "
                                      <option value='".$countryid."'>".$country."</option>
                                    ";
                                  }
                                }
                                catch(PDOException $e){
                                  echo $e->getMessage();
                                }

                                $pdo->close();
                              ?>
                        
                    </select>
                    
                </div>
            </div>
        </div>
        <div class="modal-footer">
          <button type="button" class="btn btn-default btn-flat pull-left" data-dismiss="modal" onclick = "location.reload(true);"><i class="fa fa-close"></i> Close</button>
          <button type="submit" class="btn btn-success btn-flat" name="edit"><i class="fa fa-check-square-o"></i> Update</button>
          </form>
        </div>
    </div>
</div>

查询:

$(document).ready(function() {
    $('.js-example-basic-single').select2();
    
});

  $(document).on('click', '.edit', function(e){
    e.preventDefault();
    $('#edit').modal('show');
    var id = $(this).data('id');
    getRow(id);
  });

function getRow(id){
  $.ajax({
    type: 'POST',
    url: 'city_row.php',
    data: {id:id},
    dataType: 'json',
    success: function(response){
      $("input[type=text][name=cityname]").val(response.city);
 $('#country').val("1").trigger("change");
    }
  });
}

【问题讨论】:

  • 您想在您的选择标签中添加国家(从您的 json 接收到的国家)?
  • 这是您的问题吗:我无法在 select2 下拉菜单中设置国家/地区的值?所有其他部分似乎对这部分完全是多余的。即这一行:$('#country').val("1").trigger("change");?
  • 您提供的代码工作正常:https://jsfiddle.net/4fbpjg8w/ 但是,您的问题中有一些微妙的部分似乎不是问题 1) 这对您不起作用。 2)您标记了 [bootstrap-modal](没有模式代码,所以我最初将其删除) 3)您提到 在引导程序中 - 您的意思是“在引导程序模式对话框中”吗?如果是这样,您需要向您的 select2 添加一些代码,以便它在模态中工作。这应该可以解决您的问题:select2.org/troubleshooting/common-problems
  • 我正在从与城市相关联的城市数据库中检索 country_id,并将值设置为在下拉列表中显示为城市的所选国家(此下拉列表包含国家表中的国家)
  • @freedomn-m - 出于某种原因,这对我不起作用。我确实检查了你的jsfiddle。除了我的下拉菜单实际上在模式框中的变化之外,没有任何变化。但不为我工作。我正在使用 ajax php 调用传递从数据库检索到的响应值 $('#country').val("1").trigger("change");

标签: html jquery bootstrap-modal jquery-select2


【解决方案1】:

在您的 ajax 中,您可以像这样在您的国家/地区添加选项 (我想 response.country 是国家,要么你适应):

function(response){
   var idx=1;
   countries=[];
    for(var i = 0;i< response.length;i++){
       if(countries.includes(response.country) continue;//country already in option?
       countries.push(response.country);//i keep the country in array
       $("#country").append(new Option(response.country, idx++));//add option to select
    }
     $("#country").select2();//initialize the select2
    
}

【讨论】:

  • @vineetha 你测试过我的解决方案了吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-10-05
  • 2014-12-08
  • 2018-05-25
  • 2019-08-06
  • 1970-01-01
  • 1970-01-01
  • 2021-10-14
相关资源
最近更新 更多