如何根据给定的数组将某个复选​​框设置为禁用?

时间:2018-02-08 02:37:48

标签: javascript jquery checkbox

我有一个逗号分隔的字符串数组。这将确定应该禁用whick复选框。

var lbl = "A,C";

然后我想在表单中的复选框上进行比较。复选框值为ABCD

根据给定的字符串,应禁用值为AC的复选框。

这是我目前的剧本:

$('#f_sendTo input[type=checkbox]').each(function() {

    var arr_cek_txt = $(this).val().split('||');   //This is checkboxes value
    var arr_lbl_ext = lbl.split(',');              //this is the string = "A,C"

    var val_lbl_ext;

    $.each(arr_lbl_ext,function(i){


        if(arr_cek_txt[1] == arr_lbl_ext[i]){

            $(this).prop("disabled", true);
        }           
    });                 
});

1 个答案:

答案 0 :(得分:1)

您可以执行以下操作:

使用it('http get the resource from the API and construct the weatherInfo object', function () { var $scope = $rootScope.$new(); var controller = $controller('controller', { $scope: $scope }); $scope.url = 'http://api.openweathermap.org/data/2.5/weather?q=Sydney&appid=0d00180d180f48b832ffe7d9179d40c4'; $scope.getWeatherInfo(); $httpBackend.when('GET', 'http://api.openweathermap.org/data/2.5/weather?q=Sydney&appid=0d00180d180f48b832ffe7d9179d40c4').respond(200, { data: { name: 'sydney' } }); $httpBackend.flush(); $scope.$digest(); // or $rootScope.$digest(); expect($scope.city).toEqual('sydney'); }); 循环访问该复选框。检查值,如果值在数组中,请禁用复选框。

each()
$(document).ready(function() {
  var lbl = "A,C";
  var arr_lbl_ext = lbl.split(','); /* Init this outside each. So that no need to do this every loop*/

  $('#f_sendTo input[type=checkbox]').each(function() {
    var arr_cek_txt = $(this).val(); /* Get the value of checkbox */

    if (arr_lbl_ext.indexOf(arr_cek_txt) == -1) $(this).prop("disabled", false);
    else $(this).prop("disabled", true);
  });
});