来自Ajax调用数据的js里面的str_replace

时间:2017-11-24 03:03:20

标签: javascript php ajax laravel

我想将数据循环ajax(data [i])中的字符替换为某些值, 我有这个js

<script type="text/javascript">
$(document).ready(function() {
$('select[name="parameter"]').on('change', function() {
var idpar  = $(this).val();
var subdir = $('input[name="subdirid"]').val();
var year   = $('input[name="added_year"]').val();
var i = 0;
  if (idpar != '') {
    $.ajax({
        url: "{{URL::to('myform/myformColaborate')}}/" + idpar + "/" + subdir + "/" + year,
        type: "GET",
        dataType: "json",
        success: function (data) {
           $.each(data, function (key, city2) {      
              $('select[name="type2"]').empty();
              $('select[name="type2"]').append(
               '<option disabled selected>Select Request Colaborate</option>'
                );
              for (var i = 0; i < data.length; i++) {
                $('select[name="type2"]').append(           
                  '<option value="'+ data[i] +'">Request Colaborate with '+ data[i] +'</option>' 
                );     
              } 
          });             
        }
    });
  }

});
  });
</script>

和控制器

public function myformColaborate($idpar, $subdir, $year) {
$cities = DB::table("pra_kpis")
          ->where('subdir_colaborate','like','%'.$subdir.'%')
          ->where('added_year',$year)
          ->where('kpi_parameters_id',$idpar)
          ->distinct()
          ->pluck("subdirs_id");

return response()->json($cities, 200);
}

例如,我在js之外有脚本替换,如何在js

中定义它
 <?php
$roles = DB::table('pra_kpis')->where('id','=',$l->id)->pluck('subdir_colaborate');
$dir2  = DB::table('subdirs')->select('name')->pluck('name');
$iddir = DB::table('subdirs')->select('id')->pluck('id');
     ?>       
@foreach($roles as $drop) 
{{$drop  = str_replace($iddir, $dir2, $drop)}}
@endforeach

1 个答案:

答案 0 :(得分:1)

试试这个:

仅从前端进行,

使用data[i].replace('search string', 'replace string');