我是JQuery Mobile的新手。我遇到的第一个问题是,我不能在JQuery移动端使用Javascript设置多个选择选项上的"selected"attr为"false"/unselected。
我发现的一个文档告诉我刷新一个选择,这样我就可以通过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>
我实际上想要的是当我选择"所有"选项时,其他已选择的选项变为未选择。如果你们还不明白我的意思,我将附上图片,稍后,这里已经很晚了。
谢谢。请帮帮我……:)
我想这就是你想要的
$(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>