如何在从select2中删除标签之前显示确认框?我有一个文本框,我在删除其中任何一个时选择了两个选项我想显示一条警告信息。任何人都可以帮助我吗? 在HTML中我有元素
<div class="form-group">
<label for="name" class="col-lg-3 control-label required">Select Type</label>
<div class="col-lg-9">
<select id="typeselect" class="form-control" name="crm" required>
<option value='0'>Select Type</option>
<option value='1'>Type1</option>
<option value='2'>Type2</option>
</select>
</div>
</div>
在js文件中
$("#typeselect").select2({
placeholder: 'Select Type'
});
答案 0 :(得分:0)
function deleteItem() {
if (confirm("Are you sure?")) {
$("#type option:selected").remove();
}
return false;
}
&#13;
<script src="//ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select id='type'>
<option value='1'>Type1</option>
<option value='2'>Type2</option>
</select>
<button onclick='deleteItem()'>Remove</button>
&#13;
在onClick
标记上添加option
个事件,如:
<select id='type'>
<option value='1'>Type1</option>
<option value='2'>Type2</option>
</select>
<button onclick='deleteItem()'>Remove</button>
然后您需要将confirm()
添加到deleteItem();
,如下所示:
function deleteItem() {
if (confirm("Are you sure?")) {
$("#type option:selected").remove();
}
return false;
}
请试试这个并告诉我这是否解决了您的问题!!
答案 1 :(得分:0)
以下是两种方法:
使用简单的Confirm,例如if (confirm("Are you sure?")) {// your deletion code}
使用Jquery-UI - Dialog,请参阅下面的示例
$("#typeselect").select2({
placeholder: 'Select Type'
});
$("#remove").click(function() {
var selectedval = $("#typeselect option:selected").val();
if (selectedval > 0) {
$("#dialog-confirm").dialog({
resizable: false,
height: "auto",
width: 400,
modal: true,
buttons: {
"Delete all items": function() {
$("#typeselect option:selected").remove();
$(this).dialog("close");
},
Cancel: function() {
$(this).dialog("close");
}
}
});
}
})
&#13;
#dialog-confirm {
display: none
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.3/css/select2.min.css" rel="stylesheet" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.3/js/select2.min.js"></script>
<link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
<div class="form-group">
<label for="name" class="col-lg-3 control-label required">Select Type</label>
<div class="col-lg-9">
<select id="typeselect" class="form-control" name="crm" required>
<option value='0'>Select Type</option>
<option value='1'>Type1</option>
<option value='2'>Type2</option>
</select>
<button id="remove">Remove selected value</button>
<div id="dialog-confirm" title="Delete option">
<p><span class="ui-icon ui-icon-alert" style="float:left; margin:12px 12px 20px 0;"></span>Do you wish to delete this option?</p>
</div>
</div>
</div>
&#13;