我在这个问题上创建了一个表单(使用Laravel),它的部分内容如下:
{{-- Choose a category from the radios provided, this is where I need to get the $category->id and pass it to the next element for the sub category selection
--}}
<div class="form-group">
<p><b>Choose a category:</b></p>
@foreach (App\Http\Models\GalleryCategories::all() as $category)
{!! Form::label('category_id', $category->name) !!}
{!! Form::radio('category_id', $category->id, null, ['class' => 'form-field']) !!}
@endforeach
</div>
{{-- Using the category id passed to this form-group I can then access only the sub categories
that have a gallery_categories_id = category --}}
<div class="form-group">
<p><b>Choose a sub category:</b></p>
@foreach (App\Http\Models\GallerySubCategories::all() as $sub_category)
{!! Form::label('sub_category_id', $sub_category->name) !!}
{!! Form::radio('sub_category_id', $sub_category->id, null, ['class' => 'form-field']) !!}
@endforeach
</div>
数据库设置的方式是,一个类别有许多子类别。因此,首先它将显示一个类别列表,如下所示:
cat1 () cat2 () cat3 ()
然后表单将显示如下所示的子类别:
subcat1 () subcat2 () subcat3 ()
但是我想要做的是,当某人选择一个类别时,我想捕获$ relate >id,然后立即将它传递给下一个form元素,该元素将显示与该类别相关的子类别。
如果我要将其分解并提交几个表单来来回传递数据,那么就很容易了,但是,我希望能够在不提交表单的情况下实现这一点。
JS/Ajax似乎是实现这一目标的最佳方式,我不确定。你认为如何?我还没有经验,但我想,这可能是一个相当简单的过程。
谢谢
发布于 2015-05-23 17:24:51
您可能想要的是使用jQuery并在包含类别的单选按钮上使用.change()
事件。然后获取它的值,通过AJAX将类别ID发送给服务器,并通过JSON返回它们的子类别。使用这个JSON,您只需循环它的内容,并构建包含新信息的新单选按钮。这似乎是一个相当容易的任务,根本没有必要为此提交表单。
https://stackoverflow.com/questions/30415552
复制相似问题