简体   繁体   English

删除多个选择框的选择不起作用

[英]Remove selection of multiple selectbox is not working

In jquery muliselect box remove attribute selected from selected values is not working. 在jQuery muliselect框中,删除从选定值中选择的属性不起作用。 There is no errors shows in the console 控制台中没有错误显示

the working example 工作示例

the problem was 问题是

$("#mltyslct option[value='"+foo[i]+"']").prop("selected", false); $(“#mltyslct option [value ='” + foo [i] +“']”)。prop(“ selected”,false);

is not working.But the code is not terminated. 不起作用。但是代码没有终止。

also tried 也尝试过

$("#mltyslct option[value='"+foo[i]+"']").removeAttr("selected"); $(“#mltyslct option [value ='” + foo [i] +“']”)。removeAttr(“ selected”);

but no change 但没有变化

Here is html the code 这是HTML代码

<select id="mltyslct" multiple="multiple" size="5">
    <option value="A">AA</option>
    <option value="B">AB</option>
    <option value="C">AC</option>
    <option value="1">11</option>
    <option value="2">12</option> 
     <option value="3">13</option>
</select>

and js 和js

$( document ).ready(function() {
    $("select").multiselect();
});

var strAppnd='';
var selectValue='';
var flag=true;
$( "#mltyslct" ).change(function() {
var foo = []; 

$('#mltyslct :selected').each(function(i, selected){
         foo[i] = $(selected).val(); 
    });

for (var i = 0; i < foo.length; ++i) {  
    selectValue = foo[i].substring(0, 1);  
}

    if(selectValue=="A"){         
        alert('alphabet');
        strAppnd=strAppnd+'A';
        selectValue=''
        flag=false;
    }   
    else if(selectValue=="1"){
        alert('number'); 
        if(flag==false){
        strAppnd=strAppnd+'1';
        }
        selectValue=''
     }
    console.log('val of appndStrng:'+strAppnd.substring(0, 2));
      if(strAppnd.substring(0, 2)=='A1'|| strAppnd.substring(0, 2)=='1A'){

      for (var i = 0; i < foo.length; i++) {

        $("#mltyslct option[value='"+foo[i]+"']").prop("selected", false);
           console.log('inside Booom:'+foo[i]);
      }  
    } 
});

$('#mltyslct:selected')。removeAttr(“ selected”);

This appears to be working: 这似乎正在工作:

 $(document).ready(function() { $("select").multiselect(); }); var strAppnd = ''; var selectValue = ''; var flag = true; $("#mltyslct").change(function() { var foo = []; $('#mltyslct :selected').each(function(i, selected) { foo[i] = $(selected).val(); }); for (var i = 0; i < foo.length; ++i) { selectValue = foo[i].substring(0, 1); } if (selectValue == "A") { alert('alphabet'); strAppnd = strAppnd + 'A'; selectValue = '' flag = false; } else if (selectValue == "1") { alert('number'); if (flag == false) { strAppnd = strAppnd + '1'; } selectValue = '' } console.log('val of appndStrng:' + strAppnd.substring(0, 2)); if (strAppnd.substring(0, 2) == 'A1' || strAppnd.substring(0, 2) == '1A') { for (var i = 0; i < foo.length; i++) { $("input[type='checkbox'][value='" + foo[i] + "']").prop( "checked", false ); console.log('inside Booom:' + foo[i]); } } }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <script src="http://code.jquery.com/ui/1.11.3/jquery-ui.min.js"></script> <script src="http://www.erichynds.com/examples/jquery-ui-multiselect-widget/src/jquery.multiselect.js"></script> <link rel="stylesheet" type="text/css" href="http://www.erichynds.com/examples/jquery-ui-multiselect-widget/jquery.multiselect.css"> <link rel="stylesheet" type="text/css" href="http://www.erichynds.com/examples/jquery-ui-multiselect-widget/jquery.multiselect.css"> <link rel="stylesheet" type="text/css" href="http://ajax.googleapis.com/ajax/libs/jqueryui/1/themes/ui-lightness/jquery-ui.css"> <select id="mltyslct" multiple="multiple" size="5"> <option value="A">AA</option> <option value="B">AB</option> <option value="C">AC</option> <option value="1">11</option> <option value="2">12</option> <option value="3">13</option> </select> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM