2011-12-16 51 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>