簡體   English   中英

如何在DropDownList項目的右鍵單擊上創建上下文菜單?

[英]How to create context menu on right click of DropDownList item?

我有一個從數據庫填充的asp:DropDownList 我希望能夠右鍵單擊任何一項,並通過顯示帶有該項目的那些選項的上下文菜單來刪除編輯該項目。 關於如何執行此操作或是否可行的任何想法?

正如@ freedomn-m所指出的,您需要創建自己的下拉列表。

例如,您可以執行以下操作:

添加一些html來保存下拉列表:

<button id='dog_button'>Dogs</button>
<div id='hold'></div>

添加一些CSS樣式:

body {
  font-family: arial; 
}

button {
  width: 200px;
  height: 40px;
  border: none; 
  border-radius: 3px;
  font-size: 17px;
  font-weight: bold;
  background: gold; 
  border-bottom-left-radius: 0px;
  border-bottom-right-radius: 0px;
  cursor: pointer;
  outline: 0;
}

.show {
  width: 180px;
  display: inline-block;
  background: #dedede;
  padding: 10px;
}

.square {
  border: 1px solid black;
  width: 20px;
  height: 20px;
  text-align: center;
  float: right;
  padding-top: 3px;
  cursor: pointer;
}

.ops {
  cursor: pointer;
}

使用jQuery填充下拉列表,並根據需要添加ID。 右鍵單擊您的選擇選項時, 可以使用甜蜜警報來處理彈出選項

categories_and_ops = {
    'Dog Beds' : ['Soft Dog Beds','Matress Dog Beds','Plastic Dog Beds','3 Peaks','Dog Blankets','Heating Dog Beds', 'Specific Dog Beds', 'Luxury Dog Beds'],
    'Dog Coats' : ['...','...','...','...','...','...', '...', '...']
}

$('#dog_button').click(function() {
$.each(categories_and_ops, function(cat, op) {
  div_id = 'div_' + Math.floor(Math.random() * 6);
  $('#hold').append("<div class='show' id=" + div_id + "></div><br>")
  $('#' + div_id).append("<span style='color:orange; font-weight: bold'>" + cat + "</span><div class='square'>-</div><br>" + '<br>') 
  op.forEach(function(op_elem) { 
  op_id = 'op_' + Math.floor(Math.random() * 100000); $('#' + div_id).append('&nbsp;&nbsp;&nbsp;' + "<span id=" + op_id + " class='ops'>" + op_elem + "</span>" + '<br><br>');
  $('#' + op_id).contextmenu(function() { delete_edit(); return false;    });
  })
  })
  })

$(document).on('mouseover', '.ops', function() { $(this).css('background', 'grey') })
$(document).on('mouseleave', '.ops', function() { $(this).css('background', '#dedede') })

$(document).on('click', '.square', function() {
if($(this).html() == '-') {
  $(this).parent().children('.ops').toggle();
  $(this).html('+');
  } else {
  $(this).parent().children('.ops').toggle();
  $(this).html('-');
  }
  })

function delete_edit() {
    swal({
    html: "<button id='del_button'>DELETE</button><br><br><button id='edit_button'>EDIT</button>",
    showConfirmButton : false
    }).catch(swal.noop)
    }

$(document).on('click', '#del_button', function() {
    alert('DELETED!')
})

$(document).on('click', '#edit_button', function() {
    alert('ADD YOUR EDIT CODE!')
})

結果

在此處輸入圖片說明

這是JSFIDDLE

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM