首页 > 解决方案 > How to assign dynamically fetched variable to HREF, Jquery, laravel

问题描述

I am new to development, apologies if question is basic

I am trying to add href page to the btn, but when the blade template parses url it's formatted and return error.

 $('#municipal_option').on('change',function(e){
        // console.log(e);  
        var municipal_id = e.target.value; 
        var BASEURL = "{!! url('admin/bank/') !!}";
        console.log(municipal_id);
        $.get( BASEURL + '/bankview?municipal_id=' + municipal_id, function(data){
            console.log(data);
            $("#bank_list tr").remove();
            $.each(data,function(index, bankObj){
                $('#bank_list').append('<tr class=""><td> ' + bankObj.bank_name + '</td> <td> ' + bankObj.bank_ac_no + ' </td>  <td> '+ bankObj.bank_ifsc_code +' </td> <td> <button class="btn btn-primary"> **<a href="{!! asset('bank/{{$bank->id}}/edit')!!}"> Edit </a>**  </td> <td> </td>    </tr>');


             });
});
});

The route:

| GET|HEAD  | bank/{bank}/edit   | bank.edit   | App\Http\Controllers\bankController@edit

How to point to the above route.

The url that's generated, http://localhost/public/bank/%3C?php%20echo%20e($bank-%3Eid);%20?%3E/edit

Adding to the above question, Instead of looping the result(table) in the code, can I use it independently in the blade template with hep of @foreach.

Thank you.

标签: jquerylaravellaravel-blade

解决方案


使用Javascript将自定义js值替换到路由中

var url = "{{ route('bank.edit', ':municipal_id') }}";

url = url.replace(":municipal_id", municipal_id);

所以...

 $('#municipal_option').on('change',function(e){
            // console.log(e);  
            var municipal_id = e.target.value; 
            var BASEURL = "{{ route('bank.edit', ':municipal_id') }}";
            BASEURL.replace(':municipal_id', municipal_id);
            console.log(municipal_id);
            $.get( BASEURL , function(data){
                console.log(data);
                $("#bank_list tr").remove();
                $.each(data,function(index, bankObj){
                    $('#bank_list').append('<tr class=""><td> ' + bankObj.bank_name + '</td> <td> ' + bankObj.bank_ac_no + ' </td>  <td> '+ bankObj.bank_ifsc_code +' </td> <td> <button class="btn btn-primary"> **<a href="{!! asset('bank/{{$bank->id}}/edit')!!}"> Edit </a>**  </td> <td> </td>    </tr>');


                });
            });
    });

推荐阅读