2011-12-16 53 views
0

我是新的这个JQuery Mobile的东西。我遇到的第一个问题是我不能在多选择选项中将“selected”attr设置为“false”/在JQuery mobile中使用Javascript取消选中。JQuery Mobile更改属性多个选择使用JavaScript的每个选项

我发现了一个文件叫我刷新选择,所以我可以通过JavaScript在这里操纵它在它的页面的底部: http://jquerymobile.com/demos/1.0a4.1/docs/forms/forms-selects.html

我已经做到了。但我不知道我是否做错了或什么。这是我的代码:

<script type="text/javascript"> 
function SelectAll(){ 
    var select=document.getElementById('sfruit'); 
    if (select.options[1].selected == true){ 
     alert('All Selected'); 

     for (x in select.options){ 
      if(x > 1){ 
       if (select.options[x].selected == true){ 
        alert(select.options[x].value+' selected'); 

        RefreshSelect(); 
        select.options[x].selected == false; 
       }else{ 
        alert(select.options[x].value+' unselected'); 
       } 
      } 
     } 
    } 
} 

function RefreshSelect(){ 
    var myselect = $("select#sfruit"); 
    myselect[0].selectedIndex = 5; 
    myselect.selectmenu("refresh"); 
} 
</script> 

<select onChange="SelectAll()" data-native-menu="false" id="sfruit" name="sfruit" multiple> 
    <option>Select Fruit</option> 
    <option value="all" selected>All</option> 
    <option value="apple">Apple</option> 
    <option value="orange">Orange</option> 
    <option value="grape">Grape</option> 
    <option value="melon">Melon</option> 
</select> 

我真正想要的是当我选择“全部”选项时,其他选项已被选中变为未选中。 如果你仍然不明白我的意思,我会附上图片,稍后,这里已经晚了。

谢谢你们。请帮助我。 .. :)

回答

0

我想这是你想要的

$(window).load(function(){ 
    $("select#sfruit").change(function() { 
     $("select#sfruit option:selected").each(function (obj) { 
      if($(this).index()==1){ 
       alert('All Selected'); 
       $("select#sfruit option:selected").removeAttr("selected"); 
       $("select#sfruit option:eq(1)").attr("selected",""); 
      } 
     }); 
    }); 
}); 

<select data-native-menu="false" id="sfruit" name="sfruit" multiple> 
    <option>Select Fruit</option> 
    <option value="all" selected>All</option> 
    <option value="apple">Apple</option> 
    <option value="orange">Orange</option> 
    <option value="grape">Grape</option> 
    <option value="melon">Melon</option> 
</select>