2014-02-16 24 views
0
<div id="update-checkbox" class="row update-checkbox" data-company-code="10000005980" data-type="Contractual Customer,Shipper"> 
     <input type="checkbox" value="2" data-original-title=""> 
      <div class="span3"> 
       <label class="checkbox">Contractual Customer<input type="checkbox" value="2" data-original-title=""></label> 
      </div> 
      <div class="span3"> 
       <label class="checkbox">Shipper<input type="checkbox" value="3" data-original-title=""></label> 
      </div> 
     </div> 


    var partyTypes = $("#update-checkbox").attr("data-type"); // gives "Contractual Customer,Shipper 
    $.each(partyTypes.split(','), 
     function(i, opt) { 
      //here i need to loop through all the label and checkbox inside div (id = update-checkbox) and if the party type equals to the label then check that check box 

     }); 

爲前,如果partytype =合同的客戶,然後選中相應的複選框jQuery的:遍歷一個div和comapre標籤內的所有標籤元素,檢查複選框

+0

我看不出有任何屬性'數據黨type' –

+0

應該是'數據type'。我認爲。 – Craftein

回答

2

我想,而不是通過循環數組,你可以遍歷標籤和查看它是否有像數組中

var partyTypes = $("#update-checkbox").attr("data-type")||''; 
partyTypes = partyTypes.replace(/,\s+/g, ','); 
var array = partyTypes.split(','); 
$('#update-checkbox label.checkbox').filter(function() { 
    return $.inArray($.trim($(this).text()), array) > -1 
}).find('input').prop('checked', true) 

演示:Fiddle

+0

不好用..只選擇第一個出現 – monda

+2

@monda你可以編輯小提琴重新創建案例 –

+0

請參考http://jsfiddle.net/3tjXp/2/ – monda

0

我會通過設置「的」啓動屬性在你的標籤上,然後你只需要進行一次檢查。

或者也許沒有...沒有JS =好。

然後也許風格的標籤,所以它是一個很大的可點擊區域。

2
var partyTypes = $("#update-checkbox").attr("data-type"); // gives "Contractual Customer,Shipper 
    $.each(partyTypes.split(','), 
     function(i, opt) { 

      $(".checkbox").each(function() { 
       //console.log(opt); 
       if($(this).text()==$.trim(opt)) { 


        $(this).find('input:checkbox').prop('checked',true); 
       } 
      }) ; 

     }); 

解決方案空的空間... http://jsfiddle.net/jks8S/2/