You are viewing limited content. For full access, please sign in.

Question

Question

Verify a check box when upload a file

asked on June 19, 2018 Show version history

I have a collection that contains a check box and an upload button, what I want is that by having a file loaded the check box is selected, any suggestions on how to do this?

0 0

Answer

APPROVED ANSWER
replied on July 1, 2018

This sample code assumes the class MyCollection for the collection, collectionupload for the file, and collectioncheckbox for the checkbox with a default value of "choice 1".

$('.collectionupload, .MyCollection > .cf-collection-append, button.btn.btn-danger.deleteFileBtn, .MyCollection').on("click change", checkboxuploads);
  function checkboxuploads() { 
   $(".MyCollection .kx-repeatable > div > .rpx").each(function(ind) {
    var fileuploaded = $(this).find('.collectionupload > div > div > table > tbody > tr.file').length;
    var checkbox = $(this).find('.collectioncheckbox > div > div > fieldset > span > input[value=\"choice_1\"]');
   
      if (fileuploaded) {
      checkbox.prop('checked', true);
      }
     
      else {
      checkbox.prop('checked', false);
      }
   });
  }
0 0

Replies

replied on June 19, 2018 Show version history

Hi Edgar. In this sample code, my File Upload button is field 8, so replace q8 below with the name of your upload field. In addition, I am assuming that there is only one file that can be uploaded per File Upload Field. The checkbox being checked in my sample is field 9, with the default value of "choice 1". 

When a file is uploaded, the box is checked. When it is deleted, the box is unchecked. 

$(document).ready(function() {
 
  $('#q8').on("click change", function() {

    if ($('#q8 > div > div > table > tbody > tr.file').length) {
    $('input[value=\"choice_1\"]#Field9-0').prop('checked', true).change();
    }
    else {
    $('input[value=\"choice_1\"]#Field9-0').prop('checked', false).change();
    }
    $('button.btn.btn-danger.deleteFileBtn').on("click", function() {
    if ($('#q8 > div > div > table > tbody > tr.file').length) {
    $('input[value=\"choice_1\"]#Field9-0').prop('checked', true).change();
    }
    else {
    $('input[value=\"choice_1\"]#Field9-0').prop('checked', false).change();
    }
     }); 
  }); 
});
0 0
replied on June 27, 2018

Hi,  Karina

I already implement the suggested code but it did not work for me, in the code do you use a table? is the same in the case of having it in a collection, what does my form is to fill a collection, with several documents that must be loaded, and in the same collection is a checkbox that indicates if the document is loaded.

0 0
replied on June 27, 2018

The above code is for a standalone upload field and checkbox. You will have to modify it to iterate through each row of a collection.

0 0
APPROVED ANSWER
replied on July 1, 2018

This sample code assumes the class MyCollection for the collection, collectionupload for the file, and collectioncheckbox for the checkbox with a default value of "choice 1".

$('.collectionupload, .MyCollection > .cf-collection-append, button.btn.btn-danger.deleteFileBtn, .MyCollection').on("click change", checkboxuploads);
  function checkboxuploads() { 
   $(".MyCollection .kx-repeatable > div > .rpx").each(function(ind) {
    var fileuploaded = $(this).find('.collectionupload > div > div > table > tbody > tr.file').length;
    var checkbox = $(this).find('.collectioncheckbox > div > div > fieldset > span > input[value=\"choice_1\"]');
   
      if (fileuploaded) {
      checkbox.prop('checked', true);
      }
     
      else {
      checkbox.prop('checked', false);
      }
   });
  }
0 0
replied on July 2, 2018

Hi Karina,

Thanks for the code, I tryed it and works perfectly

0 0
You are not allowed to follow up in this post.

Sign in to reply to this post.