dragonfly9527 2015-11-06 09:57 采纳率: 100%
浏览 72
已采纳

Laravel Vue.js有条件渲染

I am new to Vue.js and I want to render a form element only if another form select field is selected. I hope you understand what I mean.

Here st my Laravel Form:

<div class="form-group">
    {!! Form::label('mailarchive', 'Mailarchive: ', ['class' => 'col-sm-3 control-label']) !!}
    <div class="col-sm-6">
        {!! Form::select('mailarchive', ['-' => 'No', 'Gold' => 'Gold', 'Silver' => 'Silver', 'Bronze' => 'Bronze'], null, ['class' => 'form-control']) !!}
    </div>
</div>

<div class="form-group">
    {!! Form::label('instance', 'Instance: ', ['class' => 'col-sm-3 control-label']) !!}
    <div class="col-sm-6">
        {!! Form::select('mailarchive', ['Select' => 'Select', '1' => 'SV01', '2' => 'SV02'], null, ['class' => 'form-control']) !!}
    </div>
</div>

The second form-group (label: instance) should only be visible when 'Gold', 'Silver' or 'Bronze' in the first select field is selected, but not visible if 'No' is selected.

Thanks for your help!

Wipsly

// Update

I edited my code to this

<div class="form-group">
    {!! Form::label('mailarchive', 'Mailarchive: ', ['class' => 'col-sm-3 control-label']) !!}
    <div class="col-sm-6">
        {!! Form::select('mailarchive', ['-' => 'No', 'Gold' => 'Gold', 'Silver' => 'Silver', 'Bronze' => 'Bronze'], null, ['class' => 'form-control v-model="mailarchive"']) !!}
    </div>
</div>

<div class="form-group v-show="mailarchive !='-'"">
    {!! Form::label('instance', 'Instance: ', ['class' => 'col-sm-3 control-label']) !!}
    <div class="col-sm-6">
        {!! Form::select('mailarchive', ['Select' => 'Select', '1' => 'SV01', '2' => 'SV02'], null, ['class' => 'form-control']) !!}
    </div>
</div>

And here is my javascript

<script type="text/javascript">
    new Vue({
        el: '#mailarchive'
    })
</script>

But nothing happens. What do I wrong?

  • 写回答

2条回答 默认 最新

  • doukanhua0752 2015-11-06 17:16
    关注

    A lot to tackle here. First, you should set a "parent" Vue instance rather than creating a new Vue instance for individual input fields. For example, lets say you want to make the entire form a Vue instance, then when you open your form, set an id like this:

    {!! Form::open(['id' => 'example']) !!}
    

    Then, when you create your Vue instance, reference that id:

    <script type="text/javascript">
        new Vue({
            el: '#example'
        })
    </script>
    

    Next, this code you have is incorrect:

    {!! Form::select('mailarchive', ['-' => 'No', 'Gold' => 'Gold', 'Silver' => 'Silver', 'Bronze' => 'Bronze'], null, ['class' => 'form-control v-model="mailarchive"']) !!}
    

    Specifically, pay attention to this part: ['class' => 'form-control v-model="mailarchive"']

    What you are doing here is creating some weird class. When you specify extra HTML attributes, you need to pass an array of those attributes like this:

    {!! Form::select('mailarchive', ['-' => 'No', 'Gold' => 'Gold', 'Silver' => 'Silver', 'Bronze' => 'Bronze'], null, ['class' => 'form-control', 'v-model' => 'mailarchive']) !!}
    

    From there, another problem is how you are using v-show.

    This is what you have: <div class="form-group v-show="mailarchive !='-'"">

    Once again, for some reason, you are putting v-directives inside your class. Instead, use it as its own HTML attribute like this:

    <div class="form-group" v-show="mailarchive !== '-'">
    

    All that together, you should see something like this:

    {!! Form::open(['id' => 'example']) !!}
        <div class="form-group">
            {!! Form::label('mailarchive', 'Mailarchive: ', ['class' => 'col-sm-3 control-label']) !!}
            <div class="col-sm-6">
                {!! Form::select('mailarchive', ['-' => 'No', 'Gold' => 'Gold', 'Silver' => 'Silver', 'Bronze' => 'Bronze'], null, ['class' => 'form-control', 'v-model' => 'mailarchive']) !!}
            </div>
        </div>
        <div class="form-group" v-show="mailarchive !== '-'">
            {!! Form::label('instance', 'Instance: ', ['class' => 'col-sm-3 control-label']) !!}
            <div class="col-sm-6">
                {!! Form::select('mailarchive', ['Select' => 'Select', '1' => 'SV01', '2' => 'SV02'], null, ['class' => 'form-control']) !!}
            </div>
        </div>
        {!! Form::submit() !!}
    {!! Form::close() !!}
    </div>
    <script>
    new Vue({
        el: '#example'
    });
    </script>
    

    Here is a working example on jsfiddle: http://jsfiddle.net/zj8hwjc9/1/

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 计算二重积分∫∫e^(x+y)dxdy,其中0≤x≤1,0≤y≤1,试分别用复合辛普森公式(取n=4)以及高斯求积公式(取n=4)计算积分 给出matlab程序
  • ¥15 opencv 无法读取视频
  • ¥15 用matlab 实现通信仿真
  • ¥15 按键修改电子时钟,C51单片机
  • ¥60 Java中实现如何实现张量类,并用于图像处理(不运用其他科学计算库和图像处理库))
  • ¥20 5037端口被adb自己占了
  • ¥15 python:excel数据写入多个对应word文档
  • ¥60 全一数分解素因子和素数循环节位数
  • ¥15 ffmpeg如何安装到虚拟环境
  • ¥188 寻找能做王者评分提取的