Selectpicker with add or delete values

泪湿孤枕 提交于 2021-01-29 02:19:47

问题


I've got a bootstrap selectpicker. I've got a list of timeRange values.

<select class="selectpicker">

<option data-icon="" value="00:00-23:59">All Day (12am-11:59pm)</option>
<option data-icon="fa fa-coffee" value="05:00-08:59">Morning (5am-8:59am)</option>
<option data-icon="fa fa-briefcase" value="09:00-16:59">Business Hours (9am-4:59pm</option>
<option data-icon="fa fa-moon-o" value="17:00-20:59">Evening (5pm-8:59pm)</option>
<option data-icon="fa fa-clock-o" value="00:00-11:59">AM Hours (12am-11:59am)</option><option data-icon="fa fa-clock-o" value="12:00-23:59">PM Hours (12pm-11:59pm)</option><option data-icon="fa fa-clock-o" value="00:00-05:59">12am-5:59am</option>
<option data-icon="fa fa-clock-o" value="06:00-11:59">6am-11:59am</option>
<option data-icon="fa fa-clock-o" value="12:00-17:59">12pm-5:59pm</option>
<option data-icon="fa fa-clock-o" value="18:00-23:59">6pm-11:59pm</option>

</select>

Usecase: I want to add or remove some of them and send the resulting JSON to backend.

This is what I've tried:

 http://jsfiddle.net/M69TH/18/

I have a button called "Remove" essentially if i click that, I want to remove one of the timeRanges from the selectpicker and re-send the JSON to the backend.

How can I achieve this?


回答1:


This will work for deleting selected element form the selectpicker:

function modifications() {
  $('.selectpicker option:selected').remove();
  $('.selectpicker').selectpicker('refresh');
}

For sending json to backend:

function send() {
    var jsonData = {
         name: "value"; //change this according to your needs
    }

    $.ajax({
        url: 'backend url', //change this
        type: 'post',
        dataType: 'json',
        success: function (data) {
            // any msg you want to display on success
        },
        data: jsonData
    });
}


来源:https://stackoverflow.com/questions/39152997/selectpicker-with-add-or-delete-values

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!