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

时间:2015-05-18 12:12:10

标签: javascript jquery html multi-select

在jquery muliselect框中,从所选值中选择的删除属性不起作用。控制台中没有显示错误

工作example

问题是

  

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

无效。但代码未终止。

也尝试了

  

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

但没有变化

这是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>

和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]);
      }  
    } 
});

2 个答案:

答案 0 :(得分:1)

$(&#39; #mltyslct:selected&#39;)。removeAttr(&#34; selected&#34;);

答案 1 :(得分:1)

这似乎有效:

$(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>