简体   繁体   English

jQuery Mobile更改属性使用JavaScript的多个选择上的每个选项

[英]JQuery Mobile Change Attribute Each Option on Multiple Select Using JavaScript

I'm new with this JQuery Mobile thing. 我是JQuery Mobile的新手。 And the first problem i met in this is i can't set "selected" attr on multiple select option to "false" / unselected in JQuery mobile using Javascript. 我遇到的第一个问题是我无法在使用Javascript的JQuery mobile中将“多选”选项上的“选择”属性设置为“假” /未选择。

A documentation i found told me to refreshing a select so i can manipulate it via javascript here in the bottom of it's page: http://jquerymobile.com/demos/1.0a4.1/docs/forms/forms-selects.html 我发现的文档告诉我刷新选择,以便我可以在页面底部的javascript中通过它进行操作: http : //jquerymobile.com/demos/1.0a4.1/docs/forms/forms-selects.html

I've done it. 我做完了 But i don't know if i did it wrong or what. 但是我不知道我做错了还是什么。 Here is my code : 这是我的代码:

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

What i actually want is when i select "All" option, other option that have been selected become unselected. 我真正想要的是当我选择“全部”选项时,已选择的其他选项变为未选择状态。 I'll attaching the image if u all still doesn't understand what i mean, later, it's already late here. 如果大家仍然不明白我的意思,我会附上图片,稍后,这已经很晚了。

Thanks guys. 多谢你们。 And please help me. 并请帮助我。 .. :) .. :)

I think this is what you want 我想这就是你想要的

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

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

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