Copy the value of the Dynamic ID checkbox to the hidden input value by using jquery

综合技术 2018-07-22 阅读原文

I'm sooo... close but am stuck on one tiny thing that's probably really simple for you. The idea is to add the value of each checkbox to the hidden field's value.

The moment I add a second (or more forms) with their own variable ID's, it falls apart. I know the first part of the jQuery code works but I don't know how to get loader_id
into here.

$('#fileset-'+ loader_id +' input').click(updateFileField);

I think
that is where I'm messing up because if I hard code the id here the rest works beautifully.

The following is the full code and can also be found here

File 1
File 2 ​

Here's the jQuery portion

function updateFileField() {

 var allVals = [];
 var loader_id = $(this).parent('div').attr('id').replace('fileset-', '');
     $('#fileset-' + loader_id + ' :checked').each(function() {
       allVals.push($(this).val());
     });
     $('#file-' + loader_id ).val(allVals);
  }
 $(function() {
     $('#fileset-'+ loader_id +' input').click(updateFileField);

});

Thanks for your help!

Is that you've been looking for?

$(".fileset :checkbox").on("change", function() {
    var div = $(this).parent();
    var id = div.attr("id").split("-")[1];
    var values = [];
    div.find(":checked").each(function() {
        values.push(this.value);
    });
    $("#file-" + id).val(values.join(","));
});

DEMO: http://jsfiddle.net/fXMrj/52/

Hello, buddy!

责编内容by:Hello, buddy!阅读原文】。感谢您的支持!