简体   繁体   English

使用刀片中的参数调用 javascript function

[英]Calling javascript function with parameters from blade

I browsed through a bunch of similar sounding questions but none of them applies to my issue.我浏览了一堆类似的问题,但没有一个适用于我的问题。 I want to pass a parameter (obtained by a foreach ) to a javascript function (in the same file as my view) to feed several Select2 dropdowns in a datatable in my blade view.我想将一个参数(由foreach获得)传递给 javascript function (在与我的视图相同的文件中)以在我的刀片视图的数据表中提供几个 Select2 下拉菜单。 The issue seems to be that I can't pass the php variable $process->id to the javascript function.问题似乎是我无法将 php 变量$process->id传递给 javascript function。

From my blade view:从我的刀片视图:

@foreach($processes as $process)                        
<tr>
  <td>{{ $process->id }}</td>
  <td>{{ $process->title }}</td>
  <td>{{ $process->client }}</td>
  <td>
    <script> getDisponents($process->id); </script> //not working ofc.
    <select class="mySelect" name="state"></select> 
  </td>        
(...)
@push('js')
<script>
function getDisponents(process_id){
  $.ajax({
    url: 'http://localhost/matpro/public/getUsersPerProcess', //works.
    type: "get",
    data: {
      processId:process_id,
      //processId:'5', //works for manually given ids.
    },
    success: function(response) {
      $('.mySelect').select2({
        data: response,
      });
    },
    error: function(jqXHR, textStatus, errorThrown) {
      alert(textStatus + " : " + jqXHR.status + " : " + errorThrown);
    }
  });
}
</script>

My suggestion is separate your javascript from the php and use data attributes on the elements themselves which can be easily retrieved using javascript.我的建议是将 javascript 与 php 分开,并在元素本身上使用数据属性,这些属性可以使用 javascript 轻松检索。

Blade:刀:

<select class="mySelect" data-id="{{$process->id}}" name="state"></select> 

Then in JS use a loop to go through each instance and get it's id to pass to ajax and to be able to populate the correct element with the appropriate response:然后在 JS 中,通过每个实例对 go 使用循环并获取它的 id 以传递给 ajax 并能够使用适当的响应填充正确的元素:

var processUrl = 'http://localhost/matpro/public/getUsersPerProcess';

$('.mySelect').each(function(){ 
   // `this` is current instance of the collection
   var $sel = $(this), process_id = $sel.data('id');// from attribute

   $.get(processUrl , {processId:process_id}).then(function(response){
         // only pass data to current instance of select
         $sel.select2({data: response});
   }).catch(function(err){ 
         console.log('Ooops, something went wrong')
   });    
});

With all this being said, I don't really understand why you don't populate each one server side说了这么多,我真的不明白为什么你不填充每个服务器端

try like this像这样尝试

     <script> 
    var processId = <?php echo $process->id ?>
getDisponents(processId)
    function getDisponents(process_id){ $.ajax({ url: 'http://localhost/matpro/public/getUsersPerProcess', //works. type: "get", data: { processId:process_id, }, success: function(response) { $('.js-example-basic-single').select2({ data: fData, }); }, error: function(jqXHR, textStatus, errorThrown) { alert(textStatus + " : " + jqXHR.status + " : " + errorThrown); } }); } </script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM