2015-07-03 64 views
0

我有如下所示的倍数选择国家复选框列表: enter image description herejquery的移动动态地检查多个复选框

我有具有多个值的阵列。现在,无论选择框中存在数组中的哪个值,我都需要动态检查相同的值。我正在使用的是:

$.each(myarray, function(key, value) { 
    $('#Country option[value="'+value+'"]').attr('checked',true); 
}); 
$('#Country').checkboxradio('refresh'); 

但我无法获得选中的复选框。

FIDDLE

+0

请检查的jsfiddle在http://jsfiddle.net/s3sLhbdn/ – TechMech

+0

作品在我的iPhone:http://fiddle.jshell.net/s3sLhbdn/show/光/ – mplungjan

回答

1

的方式jQuery Mobile的作品是,它本质上使用您的选择/选项元素,创造它自己的名单,该名单是我们在你的屏幕截图看到一个。该列表不使用实际的复选框,它使用类.ui-checkbox-on和.ui-checkbox-off来模拟选择项目。

因此,我之前制作的代码片段根据第一个数组中提供的值获取所需选项的文本(国家/地区名称),然后使用它向包含文本的链接添加必要的类的国家。

var myarray = ["4","248"]; 

$(myarray).each(function(key, value){ //for each element in array 
    var textarray = [$('option[value="4"]').text(), $('option[value="248"]').text()]; //get the text of each option based on it's value (you have to make this dynamic, I hard coded this) and put it in an array 

    $('#Country-listbox ul li').find('a:contains("' + textarray[key] +'")').addClass('ui-checkbox-on').removeClass('ui-checkbox-off'); //get the link that contains the text we want (which is in the array we made before) and add the class that checks the box and remove the class that takes the check off 

}); 

Country-listbox是包含jQuery基于您的选项/选项创建的列表的div的名称。它根据你选择的元素的id来命名div。

0

我知道我的回应很晚,但它可以帮助你。你的数组可能会喜欢这个{“foo1”:“india”,“foo2”:“阿富汗”,“foo3”:“阿尔巴尼亚”};

$(document).ready(function(){ 
    var myid,myvalue; 
    var object={"foo1":"india","foo2":"Afghanistan","foo3":"Albania"}; 
    jQuery("input[name='country']").each(function() { 
    myid=this.id; 
    myvalue=this.value; 
    $.each(object,function(key,value) 
      { 
       if(value==myvalue) 
        { 
        $("#"+myid).attr("checked",true); 
        } 
      }); 

    }); 

}); 

在HTML

<body> 
<form> 
<input type="checkbox" id="foo1" name="country" value="india">india 
<input type="checkbox" id="foo2" name="country" value="Afghanistan">Afghanistan 
<input type="checkbox" id="foo3" name="country" value="Albania">Albania 
<input type="checkbox" id="foo4" name="country" value="Algeria">Algeria 
</form> 
</body> 
</html>