【问题标题】:Select Cascade Using jQuery and PHP in Laravel 4在 Laravel 4 中使用 jQuery 和 PHP 选择级联
【发布时间】:2023-04-08 12:55:01
【问题描述】:

我正在尝试根据 Laravel 4 中以前的选择框值填充选择框。这是我目前所拥有的:

我的 JS:

var url = document.location.hostname + '/cream/public/list-contacts';

var contacts;

$.ajax({
    async: false,
    type: 'GET',
    url: url,
    dataType: 'json',
    success : function(data) { contacts = data; }
});

$('#account_id').change(function() {
    alert(url);
    label = "<label class='control-label'>Contacts</label>";
    select = $("<select name='contact_id[]' id='contact_id'>");
    console.log(contacts);

    for(var i in contacts) {
        alert(contacts[i]['account_id']);
        if(contacts[i]['account_id'] == $(this).val()) {
            select.append('<option value="' + contacts[i]['id'] + '">' + contacts[i]['name'] + '</option>');
        }
    }

    $('.contacts').html(select).prepend(label);
});

我的列表-联系人路由声明:

Route::get('list-contacts', 'ContactListController@contacts');

我的 ContactListController 中的 contacts() 方法:

public function contacts()
{
    return Contact::select('contacts.id', 'contacts.account_id', DB::raw('concat(contacts.first_name," ",contacts.last_name) AS name'))->get()->toArray();
}

我认为的形式:

{{ Form::open(array('action' => 'DelegatesController@store', 'class' => 'view-only pull-left form-inline')) }}
    {{ Form::label('account_id', 'Account', array('class' => 'control-label')) }}
    {{ Form::select('account_id', $accounts) }}
    <div class="contacts"></div>
    {{ Form::label('delegate_status_id', 'Status', array('class' => 'control-label')) }}
    {{ Form::select('delegate_status_id', $delegate_statuses) }}
    {{ Form::label('price', 'Price', array('class' => 'control-label')) }}
    {{ Form::text('price', '', array('class' => 'input-small')) }}
    {{ Form::hidden('event_id', $event->id) }}
    {{ Form::submit('Add Delegate', array('class' => 'btn btn-success')) }}
{{ Form::close() }}

编辑:我已经修改了上面的代码。当我访问 /list-contacts 时,它会获得我需要的正确数据,只是没有将该数据分配给我的 JS 中的 AJAX 请求中的联系人变量?任何帮助将不胜感激。

错误:这是我的控制台日志中显示的联系人变量错误:

文件:“/Applications/MAMP/htdocs/cream/vendor/laravel/framework/src/Illuminate/Routing/Controllers/Controller.php” 线路:290 信息: ”” 类型:“Symfony\Component\HttpKernel\Exception\NotFoundHttpException”

【问题讨论】:

    标签: laravel laravel-4 eloquent drop-down-menu


    【解决方案1】:

    我现在有这个工作。这与 AJAX 请求中生成的 URL 有关。我删除了 document.location.hostname 并在没有 localhost 的情况下对 url 进行了硬编码。

    这是给感兴趣的人的工作代码:

    我的 JS:

    var url = '/cream/public/list-contacts';
    
    var contacts;
    
    $.ajax({
        async: false,
        type: 'GET',
        url: url,
        dataType: 'json',
        success : function(data) { contacts = data; }
    });
    
    $('#account_id').change(function() {
        select = $("<select name='contact_id' id='contact_id'>");
    
        for(var i in contacts) {
            if(contacts[i]['account_id'] == $(this).val()) {
                select.append('<option value="' + contacts[i]['id'] + '">' + contacts[i]['name'] + '</option>');
            }
        }
    
        $('.delegates .contacts').show();
    
        $('.delegates .contacts .controls').html(select);
    });
    

    我的列表-联系人路由声明:

    Route::get('list-contacts', 'ContactListController@contacts');
    

    我的 ContactListController 中的 contacts() 方法:

    public function contacts()
    {
        return Contact::select('contacts.id', 'contacts.account_id', DB::raw('concat(contacts.first_name," ",contacts.last_name) AS name'))->get();
    }
    

    我认为的形式:

    {{ Form::open(array('action' => 'DelegatesController@store', 'class' => 'delegates pull-left form-horizontal add-delegate')) }}
        <div class="control-group">
            {{ Form::label('account_id', 'Account', array('class' => 'control-label')) }}
            <div class="controls">
                {{ Form::select('account_id', $accounts) }}
            </div>
        </div>
        <div class="control-group contacts">
            {{ Form::label('contact_id', 'Contacts', array('class' => 'control-label')) }}
            <div class="controls">
    
            </div>
        </div>
        <div class="control-group">
        {{ Form::label('delegate_status_id', 'Status', array('class' => 'control-label')) }}
            <div class="controls">
                {{ Form::select('delegate_status_id', $delegate_statuses) }}
            </div>
        </div>
        <div class="control-group">
            {{ Form::label('price', 'Price', array('class' => 'control-label')) }}
            <div class="controls">
                {{ Form::text('price', '', array('class' => 'input-small')) }}
            </div>
        </div>
        {{ Form::hidden('event_id', $event->id) }}
    {{ Form::close() }}
    

    【讨论】:

    • 我正在尝试实施像您这样的解决方案,但我很挣扎,我的问题发布在这里:stackoverflow.com/questions/22273453/…,我在发布我的问题后看到了这个,但我不确定如何实施您的解决方案。你能帮忙吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-03-15
    • 1970-01-01
    • 2015-01-02
    • 2014-03-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多