【问题标题】:Refreshing drop down options dynamically动态刷新下拉选项
【发布时间】:2016-11-01 02:52:17
【问题描述】:

我必须处理一个表单,我必须在其中为两个字段(城市和教育机构)制作下拉菜单。当用户选择一个城市时,教育机构中的选项会更新,即出现该城市的机构。我正在后端开发 Laravel。我对事情的运作方式有一点了解,但我需要深入了解。请指导。

【问题讨论】:

  • 您需要为此使用 AJAX。去研究一下吧。

标签: ajax database laravel


【解决方案1】:

我在实现一个功能时通常会做的事情如下:

1。创建 HTML 输入字段

<select class="action-select-city"></select>

<select class="select-education"></select>

2。创建您的 jQuery 更改事件

$('.action-select-city').change(function (e) {

    var val = $(this).val();

    $education = $('.select-education');

    $.ajax({
        // Your settings etc
        success: function (data)
        {
            // Clear existing options
            $education.html('');

            // Loop data and insert new options
            for(var i = 0; i < data.length; i++)
            {
                $education.append('<option ... option>');
            }
        }
    });

});

3。创建路线

Route::post('/something', 'SomeController@someMethod')

4。创建控制器和方法

class SomeController extends Controller
{

    public function someMethod(Request $request)
    {

        // Always good to validate
        $this->validate($request, ['some rules']);

        // Get all educations in city
        $educations = .....

        return response()->json($educations);

    }

}

此解决方案显然未经测试,某些事物的命名可能会更好。我希望这个答案有帮助:)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-09-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多