如何在SELECT2中删除选定的值

时间:2018-02-09 02:46:50

标签: javascript jquery ajax jquery-select2

大家好!我的select2 jquery有问题。我使用ajax附加选项的值。 select2将显示在一个模态中。所以问题是,我想删除所选的值。我该怎么办?我在stackoverflow中尝试了很多解决方案,但我找不到答案。这是我的代码:

在HTML中

<a data-toggle="modal" data-target="#customer-ba">
    <i class="fa fa-users"></i>
    <span>Manage Customer</span>
</a>
<div class="modal fade" id="customer-ba" role="dialog">
<div class="modal-dialog">
    <div class="modal-content">
        <div class="modal-header">
            <button type="button" class="close" data-dismiss="modal" aria-label="Close">
            <span aria-hidden="true">&times;</span></button>
            <h4 class="modal-title"><i class="fa fa-object-group"></i> Customer Section</h4>
        </div>
        <form role="form" class="" method="post" action="<?php echo base_url('customer/view_customer'); ?>">
            <div class="modal-body">                    
                <div class="row">
                    <div class="form-group">
                        <label>Type of service: </label>
                        <select class="default-select2 form-control c_delivery_types" id="c_delivery_types" name="service">
                            <option></option>
                        </select>
                    </div>
                </div>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">Cancel</button>
                <input type="Submit" class="btn btn-primary pull-right"></button>
            </div>
        </form>
    </div>
</div>

IN JS

$('#customer-ba').modal({ show: false});
$(function(){
    $.ajax({
         type: 'POST',
         url: base_url+'provider/select_delivery_type/',
         success: function (data) {
              var values="";
              if(data.length != 0) {
                  $.each(JSON.parse(data), function(index,item) {
                     values+="<option 
                     value='"+item.delivery_suffix+"'>"+item.delivery_name+"
                     </option>";
                  });
               }
               $(".c_delivery_types").html(values);
          }
    });
    $('.c_delivery_types').select2({
        allowClear: true,
        placeholder: {
            id: '-1', // the value of the option
            text: 'Select an option'
        },
        dropdownParent: $("#customer-ba")
    });
    $('#customer-ba select').css('width', '100%');
});

RESULT

Result problem

1 个答案:

答案 0 :(得分:2)

试试这个

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <LinearLayout
        android:id="@+id/widget38"
        android:layout_width="51dp"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal"
        android:orientation="vertical">

        <View
            android:layout_width="match_parent"
            android:layout_marginTop="2dp"
            android:layout_height="1dp"
            android:background="@android:color/black" />

        <ImageView
            android:id="@+id/image2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:src="@drawable/image" />
    </LinearLayout>

</LinearLayout>

在此代码之后:

$(".c_delivery_types").html(values);