2017-12-01 9 views
0

$_POSTの送信後に複数選択が減少しますが、すべてのインデックスのajax応答が返されます。このフォームでは、Empresaの変更をトリガーするイベントがあり、他のすべてのフィールドがロードされ、ajaxによって返されたPHPセッション内のオプションがマークされます。複数選択は、各提出後に最後の項目の選択を解除します

各送信後、私はセッションのすべてのアイテムを持っていますが、常に各フィールドの最後のアイテムは選択解除されます。

// This code triggers an action in Empresa ... 
$(function($) {$('.dropdown-toggle').dropdown('toggle');}); 

$.ajax({ 
    url: '/ajaxrequests/requestservicos', 
    type: 'POST', dataType: 'json', 
    data: {empresas:empresas}, 
    success: function(retorno){ 
     var servs = retorno.servicos; // [10, 12, 14, 20, 21, 23, 25, 29, 32, 33, 38] 
     var checks = retorno.check; // [10, 12, 14, 20, 21, 23, 25] 
     console.log(retorno); 
     $('#ajax_servico').html(""); 
     $.each(servs, function(valor, chave){ 
      $('#ajax_servico').append($('<option>', { 
       value: valor, 
       text : chave, 
       checked: checks[valor], 
       selected: checks[valor] 
      })); 
     }); 
     $("#ajax_servico").multiselect('destroy'); 
     MultiselectSet($("#ajax_servico")); 
    } 
});   

function MultiselectSet(input){ 
    $(input).multiselect({ 
     includeSelectAllOption: true, 
     nSelectedText: 'selecionados ..', 
     nonSelectedText:'Nenhum selecionado ..', 
     allSelectedText: 'Todos selecionados ..', 
     selectAllText: 'Selecionar todos ..', 
     numberDisplayed: 1, 
    }); 
} 

enter image description here enter image description here

答えて

1

ないあなたがここで何をしたいのか確認してくださいしかし、あなたはあなたがこれを試みることができる選択するチェックであるservico内のすべての項目をしたい場合:

$.ajax({ 
    url: '/ajaxrequests/requestservicos', 
    type: 'POST', dataType: 'json', 
    data: {empresas:empresas}, 
}) 
.then(
    function(retorno){ 
    const servs = retorno.servicos, // [10, 12, 14, 20, 21, 23, 25, 29, 32, 33, 38] 
    checks = retorno.check, // [10, 12, 14, 20, 21, 23, 25] 
    $servico = $('#ajax_servico'); 
    console.log(retorno); 
    $servico.html(""); 
    servs.forEach(function(chave,index){ 
     const isInChecks = checks.indexOf(chave)!==-1; 
     $servico.append($('<option>', { 
     value: index, 
     text : chave, 
     checked: isInChecks, 
     selected: isInChecks 
     }));  
    }); 
    $("#ajax_servico").multiselect('destroy'); 
    MultiselectSet($("#ajax_servico")); 
    } 
    ,function(reject){console.error("something went wrong:",reject)} 
); 

私はオプションの値を配列のインデックスに設定する理由がわからない場合は、そのアイテムがデータベース内のアイテムのIDに設定する方が良いのでしょうか?オプションのテキストを数値に設定すると、それはidですか?もしそうなら、idとtextの値を、データベースのidを表すテキスト値に設定してください。

+0

私はあなたのコードを実行しようとするとエラーが発生します:jQuery.Deferred exception:未定義またはnullをオブジェクトに変換できませんTypeError:未定義またはnullをオブジェクトに変換できません。 –

+0

問題の画像がアップロードされました:) –

関連する問題