如何从ajax返回数据以选择标签

时间:2018-11-23 11:21:58

标签: laravel laravel-5

我在auth.register中有两个选择标签。

<div class = "form-group col-md-6 col-sm-6">
    <label for="years">Name of Course*</label>     
    <select class="form-control input-sm required" id="courses_name" name="alumni_course">
        <option value="">-- Select Any one --</option>
        @foreach($coursename as $coursenames)
            <option value="{{ $coursenames->id.','.$coursenames->coursename }}" >{{ $coursenames->coursename }}</option>
        @endforeach
    </select>
</div>

<div class = "form-group col-md-6 col-sm-6" id="dvPassport" style="display:none">
    <label for="years">Name of Branch*</label>     

    <select class="form-control input-sm required" id="branch_name" name="alumni_branch">
        @if(isset($branchname))
            @foreach($branchname as $branchnames)
                <option value="{{ $branchnames->id }}" >{{ $branchnames->branchname }}</option>
            @endforeach
        @endif
    </select>
</div>

如果从“课程名称”选项中,则任何一个都选择B.E./B.TECH。或DIPLOMA,则将显示“分支机构名称”,并且数据将与B.E./B.TECH相关。或文凭。

同一页面上的我的ajax:

<script type="text/javascript">
    $(document).ready(function() {
        $("#courses_name").change(function() {

            var courses_name1 = $("#courses_name").val();
            var courses_name2 = courses_name1.split(",");
            var id = courses_name2[0];
            var courses_name2 = courses_name2[1];
            if (courses_name2 == 'B.E./B.TECH.' || courses_name2 == 'DIPLOMA') {
                $("#dvPassport").show();
                $.ajax({
                    type: "GET",
                    url: "findbranch",
                    data: {
                        'id': id
                    },
                }).done(function(data) {
                    $("#branch_name").html(data.html);
                    //console.log(data);
                });
            } else {
                $("#dvPassport").hide();
            }
        });
    });
</script> 

路线为:

Route::get('findbranch','Auth\RegisterController@findbranch');

控制器是:

public function findbranch(Request $request)
{
    $branchname = branchname::where('course_id', $request->id)->get();
    $html = view('auth.register')->with(compact('branchname'))->render();
    return response()->json(['success' => true, 'html' => $html]);
}

但是分支名称中没有显示数据。

3 个答案:

答案 0 :(得分:1)

使用

$html = view('auth.register')->with(compact('branchname'))->render();

这将解决您的问题。

编辑:

问题{@ {1}}

应该为$("#branch_name").html(data.html);

答案 1 :(得分:0)

您可以尝试以下代码:

$。ajax({

        url: base_url + '/findbranch',
        type: "GET",
        data: {'id': id},
        dataType: 'json',
        success: function(data) {

           $("#branch_name").html(data.html);
        },

答案 2 :(得分:-1)

添加测试:

use App\branchname;

位于控制器顶部。