gpt4 book ai didi

javascript - str_replace 来自 Ajax 调用数据的 js 内部

转载 作者:行者123 更新时间:2023-12-03 02:59:47 24 4
gpt4 key购买 nike

我想将数据循环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>

和 Controller

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

最佳答案

试试这个:

仅从前端进行,

使用data[i].replace('搜索字符串', '替换字符串');

关于javascript - str_replace 来自 Ajax 调用数据的 js 内部,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47465766/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com