Split array of strings to find and set checkboxes

I have a comma separated string. I am using the javascript split command to split it into an array.

I have a ciContact checkbox group.

<table border="0" cellspacing="0" cellpadding="6">
    <tr>
        <td><label>
            <input type="checkbox" name="ciContact[]" value="Call" id="ciContact_0" />
      Call</label></td>
    <td><label>
            <input type="checkbox" name="ciContact[]" value="Email" id="ciContact_1" />
      Email</label></td>

        <td><label>
            <input type="checkbox" name="ciContact[]" value="Text" id="ciContact_2" />
      Text</label></td>
    </tr>
</table>

The array contains only those values ​​that need to be checked. These values ​​are reflected from an AJAX call that is JSON encoded. However, these values ​​(ciContact) are stored in the MySQL database as a comma-delimited array. There is a reason I did this. So, how do I know the values ​​of my comma-separated array and check the corresponding checkboxes?

I tried:

var ciContact = data.split(", ");
for (var j = 0; j < ciContact.length; j++)
    {
var selected = $('name=ciContact').find('value='+ ciContact[i]);
selected.attr("checked","checked");
}

I'm leaving on that. Ha!

Thanks guys!

+3
source share
4 answers
var ciContact = data.split(", ");
for (var j = 0; j < ciContact.length; j++) {
    $('input[name^=ciContact][value=' + ciContact[j] + ']').attr('checked','checked');`
}

Or, for a slightly better performance:

var ciContact = data.split(", "),
    $inputs = $('input[name^=ciContact]');
for (var j = 0; j < ciContact.length; j++) {
    $inputs.filter('[value=' + ciContact[j] + ']').attr('checked','checked');`
}

Or, fantasy:

var ciContact = data.split(", ").join('], [value='),
    $inputs = $('input[name^=ciContact]');

$inputs.filter('[value=' + ciContact + ']').attr('checked','checked');
+5

. , (i j).

:

var ciContact = data.split(", "),
    $elements = $('input[name="ciContact[]"]');

for (var j = 0; j < ciContact.length; j++) {
    $elements.filter('[value="' + ciContact[j] + '"]').attr("checked","checked");
}
+2
var ciContact = data.split(", ");
var checkboxes = $('input:checkbox[name="ciContact[]"]');

for (var j = 0; j < ciContact.length; j++){
    checkboxes
        .filter('[value="'+ciContact[j]+'"]')
        .attr("checked","checked");
}

0
source
var ciContact = data.split(", ");
var tmpHash = {};
for(var i = 0; i < ciContact.length; i++){
    tmpHash[$.trim(ciContact[i])] = 1; // if you are pretty sure that your data is clean, skip $.trim()
}

$('input[name="ciContact[]"]').each(function(){
    if($(this).val() in tmpHash){
        $(this).attr('checked', 'true');
    }
}

or there is a new javascript library called tog, you can use it to handle ajax.

function checkBox(data, all_options){
    var ciContact = data.split(", ");
    var tmpHash = {};
    for(var i = 0; i < ciContact.length; i++){
        tmpHash[$.trim(ciContact[i])] = 1; // if you are pretty sure that your data is clean, skip $.trim()
    }

    return Tog().map(all_options, function(opt, key, _num){
        var t = tog.Td().Tog('label').checkbox('$ciContact[]')
            .id('ciContact_', _num);
        if(opt in tmpHash){ t.checked() }

        // close the check box, and append the value as its label
        t.close().cont(opt);
        return t;
    }).html();
}

Calling checkBox (data, parameters) will give you a html fragment. and you can add it to something like $ ('table tr') On your backend, you just need to dump all your parameters into a json string. And your client side can easily get this as this so-called "all_options"

0
source

All Articles