简体   繁体   English

Select2-错误的选择值与MySQL数据

[英]Select2 - incorrect selected value with mysql data

I am using Select2 for my dropdown option. 我将Select2用于下拉选项。 I am populating it in a table form with MySQL data and the form is inside a bootstrap Modal, together with the button to edit or delete. 我使用MySQL数据以表格形式填充该表格,该表格位于引导模态内,并带有编辑或删除按钮。

<div class="btn-group">
    <a class="btn btn-primary" href="#"><i class="fa fa-home"></i> Area</a>
    <a class="btn btn-primary dropdown-toggle" data-toggle="dropdown" href="#">
        <span class="fa fa-caret-down"></span></a>
        <ul class="dropdown-menu">
        <?php
        echo "<li><a class=\"open-EditRow\" data-areano=\"".$areano."\" data-area=\"".$area."\" data-centerno=\"".$centerno."\" title=\"Edit this row\" \"><i class=\"fa fa-pencil\"></i> Edit</a></li>";
        echo "<li><a class=\"open-DeleteRow\" data-areano=\"".$areano."\" data-area=\"".$area."\" data-centerno=\"".$centerno."\" title=\"Delete this row\" \"><i class=\"fa fa-trash\"></i> Delete</a></li>";
        ?>
        </ul>
</div>

Then the Edit button will open a Modal to edit details. 然后,“编辑”按钮将打开一个模态以编辑详细信息。

<div class="form-group">
  <label class="col-sm-3 control-label">Center</label>
  <div class="col-sm-5">
  <?php                         
    $e = mysql_query("select * from center") or die(mysql_error());
  ?>
    <select class="populate placeholder" name="center" id="center">
        <option value="">-- Select a center --</option>
        <?php
        while($f=mysql_fetch_array($e))
        {
            ?>
            <option value="<?php echo $f['center_no']; ?>" <?php if ($f['center_no']==$centerno) { ?>selected<?php } ?>><?php echo $f['center']; ?></option>
            <?php
        }
        ?>
    </select>
  </div>
</div>

This is how I pass the data to the Modal: 这就是我将数据传递给模态的方法:

$('.open-EditRow').click(function(){
   var areano = $(this).attr('data-areano');
   var area = $(this).attr('data-area');
   var centerno = $(this).attr('data-centerno');
   $('#myModal #areano').val(areano);
   $('#myModal #area').val(area);
   $('#myModal #center').val(centerno);
   $('#myModal').modal('show');
});

And the Select2 code: 和Select2代码:

function DemoSelect2(){
    $('#center').select2();
}

But Select2 is displaying incorrect selected value. 但是Select2显示的选择值不正确。 It is displaying the first item generated by the query, instead of the selected value. 它显示的是查询生成的第一项,而不是所选值。

在此处输入图片说明

But upon clicking the Select2 dropdown, the selected item is highlighted correctly. 但是,在单击Select2下拉列表时,所选项目将正确突出显示。

在此处输入图片说明

What seems to be the problem here? 这里似乎是什么问题? Any guidance will be appreciated. 任何指导将不胜感激。

The workaround I did for this, and it somehow worked is to first remove the if statement which was used to compare the data. 我为此所做的解决方法,以某种方式起作用,是先删除用于比较数据的if语句。 So my option just looked like this: 所以我的选择看起来像这样:

<option value="<?php echo $f['center_no']; ?>"><?php echo $f['center']; ?></option>

And then I declared the selected value on the function DemoSelect2() 然后在function DemoSelect2()上声明选定的值

function DemoSelect2(){
    var centerno = $(this).attr('data-centerno');
    $('#center').select2("val", centerno);
}

Now, my Select2 dropdown looked like this: 现在,我的Select2下拉列表如下所示:

在此处输入图片说明

Upon clicking the dropdown, it highlighted the correct selected value: 单击下拉菜单后,它突出显示了正确的选定值:

在此处输入图片说明

I don't know if this is the correct fix for this but, this worked for me. 我不知道这是否是正确的解决方法,但这对我有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM