select2:删除按删除键选择的元素

时间:2016-03-15 20:47:52

标签: javascript jquery angularjs select2

我正在使用select2来呈现一个可编辑的选择框,但当我按下删除键时,在我的控制器中我没有检测到更改范围

<select id="mySelect" ui-select2="{ allowClear: true}"  ng-model="select2">
    <option value="one">First</option>
    <option value="two">Second</option>
    <option value="three">Third</option>
</select>

在我的控制器中我写道:

$('#mySelect').keyup(function(e){
    if(e.keyCode == 46) {
        console.log('Delete Key Pressed');
    }
}); 
有人有想法吗?

1 个答案:

答案 0 :(得分:2)

使用jquery获取选择值并使用所选值获取选项。

MyHorizontalHeader(QWidget *parent = 0) : QHeaderView(Qt::Horizontal, parent)
{
     connect(this, SIGNAL(sectionResized(int, int, int)), this, 
             SLOT(handleSectionResized(int)));
     connect(this, SIGNAL(sectionMoved(int, int, int)), this, 
             SLOT(handleSectionMoved(int, int, int)));
     setMovable(true);
}

void showEvent(QShowEvent *e)
{
    for (int i=0;i<count();i++) {
       if (!boxes[i]) {
          QComboBox *box = new QComboBox(this);
          boxes[i] = box;
       }
       boxes[i]->setGeometry(sectionViewportPosition(i), 0, 
                                sectionSize(i) - 5, height());
       boxes[i]->show();
    }
    QHeaderView::showEvent(e);
 }

void handleSectionResized(int i)
{
    for (int j=visualIndex(i);j<count();j++) {
        int logical = logicalIndex(j);
        boxes[logical]->setGeometry(sectionViewportPosition(logical), 0, 
                                       sectionSize(logical) - 5, height());
    }
}

void handleSectionMoved(int logical, int oldVisualIndex, int newVisualIndex)
{
    for (int i=qMin(oldVisualIndex, newVisualIndex);i<count();i++){
        int logical = logicalIndex(i);
        boxes[logical]->setGeometry(sectionViewportPosition(logical), 0, 
                                       sectionSize(logical) - 5, height());
    }
}

void scrollContentsBy(int dx, int dy)
{
   QTableWidget::scrollContentsBy(dx, dy);
   if (dx != 0)
      horizHeader->fixComboPositions();
}

void fixComboPositions()
{
    for (int i=0;i<count();i++)
        boxes[i]->setGeometry(sectionViewportPosition(i), 0, 
                                 sectionSize(i) - 5, height());
}

实施例。

&#13;
&#13;
var selected= $('#mySelect').val();//get value
$( "option[value='"+selected+"']" ).remove();//delete option with value selected
$('#mySelect').prop('selectedIndex',0);//clear select
&#13;
$('#mySelect').keyup(function(e){
    if(e.keyCode == 46) {
        var selected= $('#mySelect').val();
        $( "option[value='"+selected+"']" ).remove();
        $('#mySelect').prop('selectedIndex',0);
      
    }
});
&#13;
&#13;
&#13;