【问题标题】:Select Option fetch data to label using ajax in laravel选择选项在 laravel 中使用 ajax 获取数据以标记
【发布时间】:2020-12-06 06:16:47
【问题描述】:

我尝试在使用 ajax 选择 accountnumber 后获取数据,它确实有效,但在选择一次后我无法再次选择。我一直在尝试一些解决方案,但没有奏效。

AccountController.php

public function index()
{
    $AM= new AM();
    $AM->setConnection('AM');
    $key= '';
    $getData = $AM->where('Accountnumber',Auth::user()->AccountNum)->get();
    foreach($getData as $row){
        $key= $row->key;
    }


    $getAMData = $AM->where('key',$key)
                    ->select('Accountnumber')
                    ->get();

    $ci= $AM->where('Accountnumber',Auth::user()->AccountNum)
                            ->select('Accountnumber', 'cn', 'ca','ct','accountstatus')
                            ->get();
    return view('account')->with('getAMData',$getAMData)->with('ci',$ci);
}

// Methods

public function fetch(Request $request){
    if($request->ajax())
    {
        $AM= new AM();
        $AM->setConnection('AM');

        $getData = $AM->where('Accountnumber',$request->Accountnumber)->get();
        foreach($getData as $row){
            $key= $row->key;
        }

        $getAMData = $AM->where('key',$key)
                    ->select('Accountnumber')
                    ->get();

        $ci= $AccountMaster->where('Accountnumber',$request->Accountnumber)
                            ->select('Accountnumber', 'cn', 'ca','ct','accountstatus')
                            ->get();
        return view('partials._fetch')->with('getAMData',$getAMData)->with('ci',$ci);
    }
}

ajax.js

    $(document).ready(function(){
    $('#accountnum').change(function(){
        var Accountnumber = $(this).val();
        $.ajax({
            url:"/fetch",
            method: "GET",
            data: {Accountnumber:Accountnumber},
            success:function(data){
                $('#showbill').html(data);
            }
        });
    });
});

account.blade.php

        <div class="container-fluid col-sm-12">
            <h3><b> Account </b></h3>
        </div>
        </div>
        <div class="container-fluid py-4 col-sm-12" id="showbill">
                @include('partials._fetch')
        </div>

我认为我的问题出在我的控制器上,也许 fetch 方法不对。任何人都可以帮助我吗?谢谢!

_fetch.blade.php

<div class="row">
    <div class="col-md-3 col-sm-12">
        @if(count($getAMData) > 0)
        <select class="form-control form-control-md" id="accountnum" name="accountnum">
                <option value="" selected></option>
                @foreach($getAMData as $row)
                    <option value="{{ $row->Accountnumber }}">{{ $row->Accountnumber }}</option>
                @endforeach
        </select>
        @else
                <p></p>
        @endif
    </div>
    <div class="col-md-12 col-sm-12 py-4">
        <div class="card">
            <div class="card-body">
            @if(count($ci) > 0 )
                @foreach ($ci as $row )
                <div class="col-md-6 col-sm-12">
                    <label>Account Number:</label> <label><strong>{{ $row->Accountnumber }}</strong></label>
                </div>
                <div class="col-md-6 col-sm-12">
                    <label>Name:</label> <label><strong>{{ $row->cn}}</strong></label>
                </div>
                <div class="col-md-6 col-sm-12">
                    <label> Address:</label> <label><strong>{{ $row->ca}}</strong></label>
                </div>
                <div class="col-md-6 col-sm-12">
                    <label>Type:</label> <label><strong>{{ $row->ct}}</strong></label>
                </div>
                <div class="col-md-6 col-sm-12">
                    <label>Account Status:</label> <label><strong>{{ $row->ct}}</strong></label>
                </div>
                @endforeach
            @else
                <p>NO DATA FOUND</p>
            @endif
            </div>
        </div>
    </div>
    <div class="col-md-12 col-sm-12">
        <table class="table table-responsive-md table-hover ">
            <thead style="text-align: center;" class="bg-warning ">
            <tr>
                <th scope="col">(MW)</th>
                <th scope="col">Morning (0001H to 1200H)</th>
                <th scope="col">Afternoon (1201H to 1800)</th>
                <th scope="col">Evening (1801 to 2400)</th>
            </tr>
            </thead>
            <tbody>
            <tr>
                <th scope="row"></th>
            </tr>
            </tbody>
        </table>
    </div>
</div>

我尝试使用与使用 ajax 的分页相同的概念,结果并没有达到我的预期哈哈

【问题讨论】:

  • 它给了我同样的效果。也忘了提到我为此使用了两个数据库和两个表
  • 不能再次选择是指只能更改一次吗?第二次内容完全没有变化?
  • 是的,我仍在寻找解决方案,但他们使用 append,这是我正在避免的。
  • 浏览器控制台有错误吗?
  • 没有发现错误。

标签: ajax laravel routes


【解决方案1】:

好的,这是我对这个问题的解决方案。我没有包含Select Option,而是将它放在我的account.blade.php 中,这样它就不会重新生成两个选择选项。

【讨论】:

    猜你喜欢
    • 2021-08-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-13
    • 2021-02-08
    • 1970-01-01
    • 1970-01-01
    • 2012-01-28
    相关资源
    最近更新 更多