我想根据所选选项的值在多选中取消选择所选选项
我已经试过了
<select size="5" id="skills" multiple>
<option value="0">Choose - Skills</option>
<option value="19">Analytics</option>
<option value="20">Android</option>
</select>
<input type="button" id="addskills" value="Add">
<div class="skill-text">
</div>
<script>
$(function(){
$('#addskills').on('click', function(){
$('#skills :selected').each(function(i, selected){
var text = $(this).text();
var value = $(this).val();
$('.skill-text').prepend("<span class='removeskill' data-id='"+value+"'>"+text+"<i class='fa fa-times' ></i></span>");
});
});
$('body').on('click',".removeskill", function(){
var id = $(this).data("id");
//$('body').find("#skills option[value="+id+"]").removeAttr("selected");
//$('#skills :selected').find("option[value="+id+"]").removeAttr("selected");
$('#skills :selected').each(function(i, selected){
if($(this).val() == id){
alert('inside if');
$(this).removeAttr('selected');
}
});
});
})
</script>
Run Code Online (Sandbox Code Playgroud)
您可以使用 $("select option[value='"+id+"']").prop("selected", false);
它将取消选择其值设置为 id
$('body').on('click',".removeskill", function(){
var id = $(this).data("id");
$(this).remove(); // to remove that item from list
$('#skills :selected').each(function(i, selected){
if($(this).val() == id){
$("select option[value='"+id+"']").prop("selected", false); // to deselect that option from selectbox
}
});
});
Run Code Online (Sandbox Code Playgroud)
试试:https : //jsfiddle.net/p1Lu83a2/
| 归档时间: |
|
| 查看次数: |
13462 次 |
| 最近记录: |