开发者

check a checkbox in jquery

I have here a number of checkboxes. just like this:

<div id = 'fCheck'> 
  <input type="checkbox" id="mango" value="mango" /> <label>MANGO</label><br>
  <input type="checkbox" id="santol" value="santol" /> <label>SANTOL</label><br>
  <input type="checkbox" id="guava" value="guava" /> <label>GUAVA</label><br>
  <input type="checkbox" id="lomboy" value="lomboy" /> <label>LOMBOY</label><br>
  <input type="checkbox" id="apple" value="apple" /> <label>APPLE</label><br>
  <input type="checkbox" id="orange" value="orange" /> <label>ORANGE</label><br>
</div>

and I also have this data:

SupplierID     fruit_name       Granted
   10792        "mango"           "Y"
   10792        "santol"          "Y"
   10792        "guava"           "N"
   10792        "lomboy"          "N"
   10792        "apple"           "Y"
   10792        "orange"          "Y"

Now, what Im trying to do is that everytime I input that supplierID 10792 (through ajax call), all the fruits that has a Y in granted field, will be shown in the through the checkbox above, while the N will left unchecked. Can please help me out? Checking the checkboxes based on granted yes or no is problem. thanks

My ajax call:

var params = {
  "SessionID": $.cookie("SessionID"),
  "dataType":"data"
};
$.ajax({
  type: 'GET',
  url: 'processjson.php?' + $.param({path:'supplier/view',json:JSON.stringify(params)}),
  dataType: primeSettings.ajaxDataType,
  success: function(data) {
    if ('error' in data)
    {
      showMessage('ERROR: ' + data["error"]["msg"]);
    }
    else{ 
      $.each(data['result']['main']['rowdata'], function(rowIndex, rowDataValue) {
      var groupFlag=0;
        $.each(rowDataValue, function(columnIndex, rowArrayValue) {
          var fldName = data['result']['main']['metadata']['fields'][columnIndex].name;
          if (fldName == 'supplier_id'){
            supplierID = rowArrayValue; //alert(rightCode);
            if (supplierID == SupID){//check if the supplier found
              groupFlag=1;
            }
          }
          if (fldName == 'fruit_name'){
            fruitname = rowArrayValue;
          }
          if (fldName == 'granted'){
            grant = rowArrayValue;
            if (groupFlag ==1){
              if (hasRight == 'y')
              $('#dialogUserGroupEdit').append('<input type="checkbox" id="'+ rightGroupCode +'" value="'+ rightDesc +'" /> <label>'+rightDesc+开发者_开发技巧'</label><br>');
              //must check the checkbox here that have the same fruit_name in the html

            }
          }
        });
      });
    }
  }      
}) 


I have figured it out now. Here's what happen. Everytime I input the 'supplier ID', it will call my ajax and check my supplier table (only under that ID) if the fruits is granted or not. If i see a fruit that is granted, it will loop all the checkboxes I have and check the fruitsID if it exist. If found, then check box will be check.

here's my answer that greatly work for me:

               $("input:checkbox").each(function(){
                  if ($(this).attr("id") ==  fruitName){
                     $(this).attr("checked",true);
                  }                                     
               });

And here is my complete ajax call.

var params = {
  "SessionID": $.cookie("SessionID"),
  "dataType":"data"
};
$.ajax({
  type: 'GET',
  url: 'processjson.php?' + $.param({path:'supplier/view',json:JSON.stringify(params)}),
  dataType: primeSettings.ajaxDataType,
  success: function(data) {
    if ('error' in data)
    {
      showMessage('ERROR: ' + data["error"]["msg"]);
    }
    else{ 
      $.each(data['result']['main']['rowdata'], function(rowIndex, rowDataValue) {
      var groupFlag=0;
        $.each(rowDataValue, function(columnIndex, rowArrayValue) {
          var fldName = data['result']['main']['metadata']['fields'][columnIndex].name;
          if (fldName == 'supplier_id'){
            supplierID = rowArrayValue; //alert(rightCode);
            if (supplierID == SupID){//check if the supplier found
              groupFlag=1;
            }
          }
          if (fldName == 'fruit_name'){
            fruitname = rowArrayValue;
          }
          if (fldName == 'granted'){
            grant = rowArrayValue;
            if ((groupFlag ==1) && (granted == 'Y')){
               $("input:checkbox").each(function(){
                  if ($(this).attr("id") ==  fruitName){
                     $(this).attr("checked",true);
                  }                                     
               });
            }

          }
        });
      });
    }
  }      
}) 


While creating the markup for these checkboxes you can add a selected="selected attribute in the input tag for those items where Granted == "Y". You dont have to do this through jquery on page load.


IF Granted="Y"
<input checked="checked" />

just add checked="checked" wherever you want the checkbox to be checked by default.


You could try this (not tested) -

   $.each(results, function() {
       if (this.Granted === 'Y') $('input:checkbox[id=' + this.fruit_name  + ']').attr('checked',true);
    });


You will need to scan your table and check the box for each found row.

$('#table tr').each(function() {
   var name = $(this).find('td.eq(2)').text();
   var granted = $(this).find('td.eq(3)').text();

   var chk = $('#fCheck #'+name); //$('#fCheck input[value="'+name+'"]');

   if (granded.toLowerCase() == 'y') {
      chk.attr('checked', true);
   } else {
      chk.removeAttr('checked');
   }
});

Note that this selects every row of the table, you will need to narrow this to the rows only containing your supplier id in the first column if the table contains more rows.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜