【问题标题】:Ajax & Laravel - cannot populate a textbox base on a dynamic dropdown selectAjax 和 Laravel - 无法基于动态下拉选择填充文本框
【发布时间】:2018-12-30 11:41:31
【问题描述】:

我在文本框中显示 ajax 数据时遇到问题,这是我唯一的问题,我无法在文本框中显示数据,请参阅我的

控制器中的代码

public function create()
{
    $aircraft = Aircraft::all();
    $aircraft_reg = Aircraft::pluck('aircraft_registration_number', 'id')->toArray();
    return view('admin.aircrafts.create',compact('aircraft_reg','aircraft'));
}

public function findPrice(Request $request) {
    $p = Aircraft::select('aircraft_id')->where('id',$request->id)->first();
    return response()->json($p);
}

路由中的代码

Route::get('/admin/aircrafts/findPrice', 'Admin\AircraftsController@findPrice');

查看代码

 {{Form::select('productname', $aircraft_reg,null,['class' => 'form-control productname', 'placeholder' => 'Select RPC No.'])}}<br>
    <br>
    {{Form::text('prod_price', '', ['class' => 'form-control prod_price'])}}

AJAX 中的代码

 <script type="text/javascript">

  $(document).ready(function(){
        $(document).on('change','.productname',function(){
            var prod_id=$(this).val();

            var a=$(this).parent();
            console.log(prod_id);
            var op="";

            $.ajax({
            type:'get',
            url:'{!! URL::to('admin/aircrafts/findPrice') !!}',
            data:{'id':prod_id},
            dataType:'json',//return data will be json
            success:function(data){
                console.log("aircraft_id");
                console.log(data.aircraft_id);

                // here price is column name in products table data.coln name

                a.find('.prod_price').val(data.aircraft_id);
            },
            error:function() {
            }
        });
     });
  });
  </script>

我的控制台上有一个警告。

这里是警告

[Violation] 向滚动阻塞添加了非被动事件侦听器 “鼠标滚轮”事件。考虑将事件处理程序标记为“被动”以 使页面更具响应性。看 https://www.chromestatus.com/feature/5745543795965952create:342

这是输出,但没有显示任何内容

【问题讨论】:

  • 请在此处粘贴 Ajax 调用后的响应

标签: php jquery mysql ajax laravel


【解决方案1】:

你的 Laravel json 应该有关联数组。像这样的东西: return json_encode(array("aircraft_id"=>$p));

【讨论】:

  • 我会替换它吗?
  • Console.log(data)
【解决方案2】:

请尝试更改此设置 $(document).on('change','.productname',function(){

由此

$('.productname').on('change',function(e){

用 e 代替这个

请试试这个,让我如何进行研讨会:)

【讨论】:

  • 仍然没有工作,先生.. 感谢您的回复,但它不起作用
【解决方案3】:

在 jquery 中进行此更改。

删除 var a=$(this).parent(); 将数据:{'id':prod_id},更改为数据:{id:prod_id}, 更改 a.find('.prod_price').val(data.aircraft_id);到 $('.prod_price').val(data.aircraft_id);

【讨论】:

    【解决方案4】:

    请遵循此代码结构 $(document).ready(function(){

    $('.productname').on('change',function(e){
    e.preventDefault();
    var prod_id=$(this).val();
    $.ajax({
    type:'get',
    url:'{!! URL::to('admin/aircrafts/findPrice') !!}',
    data:{id:prod_id},
    dataType:'json',//return data will be json
    success:function(data){
    console.log(data.aircraft_id);
    $('.prod_price').val(data.aircraft_id);
    
    },
    error:function(){
    alert("Error Occurred");
    }
    });
    
    
    });
    

    });

    【讨论】:

    • 你能粘贴这个console.log(data)的结果吗? , 请。我想知道服务器的响应是什么
    【解决方案5】:

    //确保你的 html 头文件中有这个

    //还要确保在你的ajax代码之前你的jquery脚本中有这个脚本

    $.ajaxSetup({
        headers: {'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')}
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-12-21
      • 2019-06-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-05-18
      • 1970-01-01
      相关资源
      最近更新 更多