Send more than one checkbox value using Jquery.Multiselect

Asked

Viewed 20 times

-3

I was able to send the values of a checkbox to the controller using the function below:

function InformacoesAdicionais() {
    var listOfRegiao = [];
    $.each($("#select-regiao option:selected"),
        function (index, data) {
            listOfRegiao.push($(this).val());
           
        });

    $.ajax(
        {
            async: true,
            dataType: 'JSON',
            type: 'POST',
            url: '/Campanha/InformacoesAdicionais',
            contentType: 'application/json; charset=utf-8',
            data: JSON.stringify({ listOfRegiao: listOfRegiao }),
            success: function (data) {
                if (data.Success) {
                    
                }

            },
            error: function () {
                alert("Ocorreu um erro ao salvar os dados, tente novamente.")
            },
        });


}

<select id="select-regiao" multiple="multiple">
        <option value="Capital">Capital</option>
        <option value="Interior">Interior</option>
        <option value="Vale">Vale</option>
        <option value="Litoral">Litoral</option>

    </select>

However, I need to send the values of another checkbox in the same function simultaneously. I tried several ways but always sends only the value of the checkbox "select-regiao" while the other is null.

Second checkbox:

 <select id="select-canal" multiple="multiple">
        <option value="Farma">Farma</option>
        <option value="Perfumaria">Perfumaria</option>
        <option value="Alimentar">Alimentar</option>
    </select>
  • Here data: JSON.stringify({ listOfRegiao: listOfRegiao }) you are only sending a select. Not to mention that there is no checkbox in the code.

  • I am using Jquery Multiple to create checkboxes in the view. When the user marks one or more values from the list, the function collects the name of the fields and creates an array of values.

1 answer

0

I got it this way

 function InformacoesAdicionais() {
    var listOfRegiao = [];
    var listOfCanal = [];
    $.each($("#select-regiao option:selected"),
        function (index, data) {
            listOfRegiao.push($(this).val());
           
        });
    $.each($("#select-canal option:selected"),
        function (index, data) {
            listOfCanal.push($(this).val());

        });
    $.ajax(
        {
            async: true,
            dataType: 'JSON',
            type: 'POST',
            url: '/Campanha/InformacoesAdicionais',
            contentType: 'application/json; charset=utf-8',
            data: JSON.stringify({ listOfRegiao: listOfRegiao, listOfCanal: listOfCanal }),
            success: function (data) {
                if (data.Success) {
                    
                }

            },
            error: function () {
                alert("Ocorreu um erro ao salvar os dados, tente novamente.")
            },
        });


}
  • Please provide additional details in your reply. As it is currently written, it is difficult to understand your solution.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.